Laravel migrate creating table with more than one primary key

感情迁移 提交于 2020-01-15 07:06:07

问题


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:


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.




回答2:


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();
    });


来源:https://stackoverflow.com/questions/23098207/laravel-migrate-creating-table-with-more-than-one-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!