我使用此设置进行了迁移:
$table->increments('id');
$table->integer('user_id', 10)->unsigned(); // this is meant to be used as a foreign key
Run Code Online (Sandbox Code Playgroud)
做了php artisan migrate后,它返回一个错误:
[Exception]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition;
there can be only one auto column and it must be defined as a key (SQL: create table `transactions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null auto_increment primary key) default character set utf8 collate utf8_unicode_ci) (Bindings: array ())
Run Code Online (Sandbox Code Playgroud)
我没有将user_id指定为auto_increment主键,但是Migration会将其视为auto_increment主键.
如何在迁移中创建外键?