Laravel迁移创建具有多个主键的表


Laravel migrate creating table with more than one primary key

我发出了命令:

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

当它开始迁移时,它以错误结束:

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))

模式中有一个Customer模型和相应的表,我正试图用customer_id来引用它。但是,它不应该是orders表的主键。

以下是阻止迁移运行的up()代码:

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

我该如何让它发挥作用?

问题是使用带有第二个参数的函数->integer()。根据文档,第二个参数需要布尔值来设置是否自动递增:bool $autoIncrement = false)。

试试这个:

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

一个可能的问题可能是:

从这里

问题的根源是:具有外键的列必须和该键的类型相同。你有不同的类型:INT/UNSIGNED INT

关于Laravel的文件也提到了这一点:注意:当创建引用递增整数的外键时,请记住始终使外键列无符号。