我发出命令:
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_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()
与第二个参数一起使用。根据docs第二个参数需要一个 bool 值来设置是否自动增量: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();
});
关于php - Laravel迁移创建具有多个主键的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098207/