Framework: [5.4]Eloquent Mysql Foreign key Failure increments integer pair errno 150

Created on 3 Apr 2017  路  1Comment  路  Source: laravel/framework

  • Laravel Version: 5.4.17
  • PHP Version: 5.6
  • Database Driver & Version:
    MYSQL -> 5.5.42

Description:

MYSQL will fail to pair columns to create foreign keys that are not of the same type and defined length. The failure occurs when pairing increments which has a primary key and index does not pair with the same column type without an index already on that column in that table.
ie. table X has no FK on column post_id int=(11) and posts has id with a primary key. increments = int(10).

Eloquent creates a migration with int(10) on increments on primary keys but when integer is used it is int(11). This throws a mysql error.

If the same type and length of int is used making a FK will throw an error because there is no index for on column post_id as an example. Also i dont want to have to create indexes just so i can create a FK. It should be done in one command.

Steps To Reproduce:

`Schema::create('questions', function ( Blueprint $table ) {

       $table->increments('display_order_id');
            $table->integer('provider_id');
            $table->integer('next_question');
            $table->integer('question_id');
            $table->integer('display_order');
    });`

Migrate with this code in a new migration file and you will see on any MYSQL GUI manager the lengths or bytes are wrong, they dont match.
See example from link from my database.
http://imgur.com/a/kwG8B

Most helpful comment

Default for integer is 11, default for unsigned integer is 10. You need to use unsignedInteger to create a foreign key index.

        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');;
        });

>All comments

Default for integer is 11, default for unsigned integer is 10. You need to use unsignedInteger to create a foreign key index.

        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');;
        });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhiloNL picture PhiloNL  路  3Comments

JamborJan picture JamborJan  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

felixsanz picture felixsanz  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments