Laravel 4 迁移错误 - 创建两个 auto_increment 主键字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17876491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Migration error - creates two auto_increment primary keys fields
提问by Sandyandi N. dela Cruz
I made a migration with this setup:
我使用此设置进行了迁移:
$table->increments('id');
$table->integer('user_id', 10)->unsigned(); // this is meant to be used as a foreign key
After doing php artisan migrate it returns an error:
执行 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 ())
I didn't specify user_id to be an auto_increment primary key but Migration treats it as so.
我没有将 user_id 指定为 auto_increment 主键,但 Migration 将其视为如此。
How can I make a foreign key in Migrations?
如何在 Migrations 中创建外键?
回答by Sandyandi N. dela Cruz
@crynobone: Second parameter are for boolean use to determine primary key, there no length option for integer.
@crynobone:第二个参数用于确定主键的布尔值,整数没有长度选项。
Refer here: https://github.com/laravel/laravel/issues/2212#issuecomment-21608193
参考这里:https: //github.com/laravel/laravel/issues/2212#issuecomment-21608193
回答by Adham Saad
In Laravel 4 , second parameter in integer function is for indicating the integer column to be auto incremented or not ( and therefore primary key or not ) In my case , to add an auto incremented id in a table I write it like that
在 Laravel 4 中,整数函数中的第二个参数用于指示整数列是否自动递增(因此主键与否)在我的情况下,要在表中添加自动递增的 id,我是这样写的
$table->integer('id' , true);
It creates an integer column with 11 digits .
它创建一个具有 11 个数字的整数列。
回答by Andreyco
Why not specify user_id
as primary key with autoincrement?
为什么不指定user_id
为带有自动增量的主键?
$table->increments('user_id');
// other columns
...
Schema builder create user_id
, which is 10 digits long, unsigned & primary key.
Schema builder create user_id
,长度为 10 位,无符号和主键。