2

I issued the command:

php artisan generate:scaffold order --fields="customer_id:integer(11), order_date:date, notes:text”

When it gets to migrate, it ends with the error:

General error: 1 table "orders" has more than one primary key (SQL: create table "orders" ("id" integer not null primary key autoincrement, "customer_id" integer not null primary key autoincrement, "order_date" date not null, "notes" text not null))

There is a Customer model and corresponding table that I am trying to reference with customer_id in the schema. However, it shouldn't be the primary key for the orders table.

Here is the up() code that is preventing migration from running:

    Schema::create('orders', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_id', 11);
        $table->date('order_date');
        $table->text('notes');
        $table->timestamps();
    });

How do I get this to work?

1
  • the generate:scaffold commands is generating the sql with 2 primary keys ( customer_id and id ) . Are you sure that's the up migrate that is not working ? Do you currently have a orders table ? Commented Apr 16, 2014 at 5:06

2 Answers 2

7

The problem is that you use the function ->integer() with a second param. According to the docs the second parameter expects a boolean to set autoincrement or not: bool $autoIncrement = false).

Try this:

Schema::create('orders', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_id');
        $table->date('order_date');
        $table->text('notes');
        $table->timestamps();
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I think this should be the right answer, as it clearly was the issue here.
1

A possible problem might be :

from here

Root of problem is : column with foreign key must be same type as that key. And you have different types: INT/UNSIGNED INT

The documentation on Laravel mentions this also : Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.