Skip to content
Advertisement

Add foreign key to a migration (Laravel)

I´ve spent the whole day trying to figure out what is happening with the following migration.

This the migration of the table. My idea is to use ‘id_stays_abroad’ and ‘user_id’ as foreign keys.

JavaScript

This is the other migration to add the foreign keys

JavaScript

When I run php artisan migrate I got the following error:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table booking__stays__abroads add constraint booking__stays__abroads_id_stays_abroad_foreign foreign key (id_stays_abroad) references stays_abroads (id))

Thanks in advance, guys.

Advertisement

Answer

You are not using the same column definition for id and id_stays_abroad. Use unsignedBigInteger instead of unsignedInteger.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement