Setting a foreign key bigInteger to bigIncrements in Laravel 5.4
Reason is primary and foreign references must be of same type.
bigIncrements()
wants unsignedBigInteger()
and increments()
wants unsignedInteger()
If you are using
$table->bigIncrements('id');
as a primary key in a users table:
then use
$table->unsignedBigInteger('user_id'); as foreign key
And if you are using $table->increments('id');
as a primary key in a users table then use
$table->unsignedInteger('user_id'); as freign key.
$table->increments(); creates just integer and $table->bigIncrements(); creates big integer.
so the reference type must be the same.
Read more https://laravel.com/docs/4.2/schema#adding-columns
Remove unsigned()
from:
$table->bigIncrements('id')->unsigned();
And the other migration should look like this:
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
....
$table->index('user_id');
The problem is that bigIncrements
returns an unsignedBigInteger.
In order to work the Foreign key must be an unsigned big integer with index()
.