Daniel
Daniel

Reputation: 13

Laravel 5.8: Migrations Foreign key constraint is incorrectly formed

I created two tables and in the second one I set up a foreign key. When I call the comand php artisan migrate I get following error:

Foreign key constraint is incorrectly formed

The first table name is members and the second partners

$table->integer('member_id')->unsigned();
$table->foreign('member_id')->on('members')->references('id')->onDelete('cascade')->onUpdate('cascade');

Upvotes: 0

Views: 683

Answers (3)

fedev
fedev

Reputation: 426

$table->integer('member_id')->unsigned();

change as

$table->bigInteger('member_id')->unsigned();

becouse laravel use as default bigIncrement for id columns in migration files.

Upvotes: 1

SuperDJ
SuperDJ

Reputation: 7661

First of all since Laravel 5.8 all id are bigintegers

However since they are also unsinged you can do:

$table->unsignedBigInteger('member_id');

For the second line you might want to change the order:

$table->foreign('member_id')->references('id')->on('members')->onDelete('cascade')->onUpdate('cascade');

Do note that the members table should be created first before you can reference a key of it.

Upvotes: 0

AH.Pooladvand
AH.Pooladvand

Reputation: 2069

Your members table should be created before your current table
or maybe your memers id field has different type than your current table
please post your migration files

Upvotes: 0

Related Questions