Skip to content
Advertisement

How to use foreign keys migration with database migrations. (errno: 150 “Foreign key constraint is incorrectly formed”)?

CodeIgniter 4 has a handy solution for migrations and seeders. Without the usage of foreign keys, everything is working perfectly. But when I use foreign keys I get “Unable to add foreign key”.

This is because of the order of happenings:

Quick example:

JavaScript

So when I now run php spark migrate or php spark migrate:refresh the foreign key can not be set because the bar table is created, but the referenced foo table does not exist yet.

Technically I could run a separate function that I run after my migration, but I like the one command of php spark migrate and everything is done.

What is the correct way to solve this issue?

This is my create table created code:

JavaScript

Addendum

I think I find the direction of the issue. Noticed from the comments, it is correct, the order of creation of the tables is not correct. This can be fixed by changing the timestamps in the filename, so that foo gets created before bar. But as this is not fixing the issue, I found something else: This is code I’m using to migrate bar:

JavaScript

This generates a fooid int(11) UNSIGNED NOT NULL. The issue is, that foodid can not be null but the fk sets the value to null on delete.

But… the default for null of a field is 'null' => true and even if I add this manually, it is not generating the nullable field.

Advertisement

Answer

Simply rename your migration timestamp prefixes such that the table creation of foo comes before that of bar. For example, if the migration file names are as follows:

JavaScript

Rename their migration timestamp prefixes in that the referenced table (foo) comes first.

JavaScript

Lastly, rerun the pending migrations. php spark migrate.


Addendum 1

In reference to your newly edited question description, move the table creation query of foo to come before that of bar. I.e:

JavaScript

Addendum 2

2022-02-16-101819_CreateFooMigration.php

JavaScript

2022-04-22-101819_CreateBarMigration.php

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