Laravel Scheme Builder is adding auto_increment to all integer fields which makes it fail

别说谁变了你拦得住时间么 提交于 2019-12-04 17:29:57

Hi here's probably a solution. Lets look into this line which defines user_id value.

$table->integer('user_id', 10)->unsigned();

I think you've meant that you need 10 byte unsigned integer writing this, but there's one thing about integer field type in Laravel 5.3. Lets look into this method definition, it expects up to 3 parameters to be passed in:

      public function Blueprint::integer($column, $autoIncrement = false, $unsigned = false) Illuminate\Support\Fluent 

      Create a new integer (4-byte) column on the table.
      Parameters: 
      string $column
      bool $autoIncrement
      bool $unsigned

So by passing in 10 php casts it to boolean and it makes it true, that is why it says you are trying to create more than one autoincrement fields!

Finaly the solution is:

    $table->integer('user_id', false,true);

with this you are asking to create not autoincrement field but still unsigned as you wish. But it creates 4byte unsigned integer with it so. There is even better solution:

   $table->bigInteger('user_id',false,true);

This one creates 8byte unsigned NOT autoincrement field in database.

Hope it'll help.

In your code you are trying to give sizes for integer which leads to the error. If you change the following code, you won't get the same error.

Change this line $table->integer('user_id', 10)->unsigned(); to $table->integer('user_id');

Hope this will help

I fixed by adding $table->index('user_id'); to my tag_list table schema builder

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