Laravel 4 Migration错误 - 创建两个auto_increment主键字段

San*_*ruz 11 database-migration laravel laravel-4

我使用此设置进行了迁移:

$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主键.

如何在迁移中创建外键?

San*_*ruz 21

@crynobone:第二个参数用于布尔值确定主键,整数没有长度选项.

请参阅此处:https://github.com/laravel/laravel/issues/2212#issuecomment-21608193

  • nvm,在这里找到答案http://stackoverflow.com/questions/22077573/laravel-migration-will-not-add-foreign-key你应该使用$ table-> integer('app_group_id') - > length(10) - >无符号(); 在Laravel 4 (2认同)