2013-07-26 72 views
11

我犯了一个移民与此设置:Laravel 4迁移错误 - 创建两个AUTO_INCREMENT主键字段

$table->increments('id'); 
$table->integer('user_id', 10)->unsigned(); // this is meant to be used as a foreign key 

做PHP的工匠后迁移它返回一个错误:

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

我没有指定user_id为auto_increment主键,但Migration将其视为如此。

如何在迁移中创建外键?

回答

21

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

参考这里:https://github.com/laravel/laravel/issues/2212#issuecomment-21608193

+0

那么你如何处理外键? – jrenouard

+2

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

-2

为什么不指定user_id与自动增量的主键?

$table->increments('user_id'); 
// other columns 
... 

模式生成器创建user_id,这是10个数字长,无符号&主键。

3

在Laravel 4中,整数函数中的第二个参数用于指示整数列是否被自动递增(也就是说主键与否) 在我的情况下,要在表中添加一个自动递增的ID,我写它像

$table->integer('id' , true); 

它创建与11位的整数的列。