Laravel 4 Migration error - creates two auto_increment primary keys fields

霸气de小男生 提交于 2019-12-22 03:40:58

问题


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:

[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.

How can I make a foreign key in Migrations?


回答1:


@crynobone: Second parameter are for boolean use to determine primary key, there no length option for integer.

Refer here: https://github.com/laravel/laravel/issues/2212#issuecomment-21608193




回答2:


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

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

It creates an integer column with 11 digits .




回答3:


Why not specify user_id as primary key with autoincrement?

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

Schema builder create user_id, which is 10 digits long, unsigned & primary key.



来源:https://stackoverflow.com/questions/17876491/laravel-4-migration-error-creates-two-auto-increment-primary-keys-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!