Laravel 4 迁移错误 - 创建了两个自增主键字段

11

我使用了以下这个设置进行了迁移:

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

在运行 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 ())

我没有将user_id指定为自增主键,但迁移(Migration)似乎把它当作了自增主键。

在迁移中如何创建外键?

3个回答

21

那么你如何处理外键? - jrenouard
2
找到答案了,可以在Laravel 4中使用$table->integer('app_group_id')->length(10)->unsigned(); 来添加外键。具体解答请参考此链接:https://dev59.com/zH3aa4cB1Zd3GeqPXghm - jrenouard

3

在Laravel 4中,整数函数的第二个参数用于指示是否自动递增整数列(因此是主键还是非主键)。

在我的情况下,为了在表中添加自动递增的ID,我会像这样编写:

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

它创建了一个拥有11个数字的整数列。


-2
为什么不将user_id指定为自增主键?
$table->increments('user_id');
// other columns
...

架构生成器创建了user_id,它是10位数字长,无符号且为主键。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接