MySql – Cannot delete or update a parent row: a foreign key constraint fails

Have you ever tried to make a relation with a foreign key, and when inserting, you get this error?

Googling this will give you many possible solutions:

  • foreign key does not exist in the related table
  • you have invalid data in one of the tables
  • your foreign key relation is backwards
  • disable foreign key checks (not cool)

For me, none of these worked.

By mistake, I found the real problem: One of the related tables had a different storage engine.

The Solution: Set the same … storage engine (ex: InnoDB) to all your tables involved in that relationship.
You could use the following command:

Hope this helps!