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.
`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
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');;
});
Most helpful comment
Default for integer is 11, default for unsigned integer is 10. You need to use
unsignedIntegerto create a foreign key index.