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

跟風遠走 提交于 2020-01-13 06:25:09

问题


When I try to use PHP Laravels Database Migration and schema builder I keep getting the error below on any tabvle that has an auto incrementing ID column and a regular user_id integer column.

The error below shows that the user_id column SQL is being generated with the value auto_increment on the user_id and my code does not tell it to do that anywhere!

I am using Laravel v5.3

My schema code is:

public function up()
{
    Schema::create('bookmark_tag_lists', function(Blueprint $table)
    {
        $table->increments('id', 10);
        $table->string('title', 100)->nullable();
        $table->string('slug', 100)->nullable();
        $table->text('description', 65535)->nullable();
        $table->string('list_icon', 200)->nullable();
        $table->text('tags', 65535)->nullable();
        $table->integer('user_id', 10)->unsigned();
        $table->dateTime('created_on');
        $table->dateTime('modified_on');
        $table->integer('parent')->default(0);
        $table->string('breadcrumb_path')->nullable();
        $table->integer('tag_count')->default(0);
        $table->integer('bookmark_count')->default(0);
        $table->integer('sort')->default(0);
        $table->integer('active')->default(1);
    });
}

Database error

[Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table def
  inition; there can be only one auto column and it must be defined as a key
  (SQL: create table `bookmark_tag_lists` (`id` int unsigned not null auto_in
  crement primary key, `title` varchar(100) null, `slug` varchar(100) null, `
  description` text null, `list_icon` varchar(200) null, `tags` text null, `u
  ser_id` int unsigned not null auto_increment primary key, `created_on` date
  time not null, `modified_on` datetime not null, `parent` int not null defau
  lt '0', `breadcrumb_path` varchar(255) null, `tag_count` int not null defau
  lt '0', `bookmark_count` int not null default '0', `sort` int not null defa
  ult '0', `active` int not null default '1') default character set utf8 coll
  ate utf8_unicode_ci)



  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table def
  inition; there can be only one auto column and it must be defined as a key

回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/41078059/laravel-scheme-builder-is-adding-auto-increment-to-all-integer-fields-which-make

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