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?
Cannot delete or update a parent row: a foreign key constraint fails

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:
ALTER TABLE  `your_table` ENGINE = INNODB

Hope this helps!

Comments

Popular posts from this blog

Improve font rendering for IntelliJ IDEA / Pycharm in Ubuntu

Fix - Updated from OSX El Capitan Gold Master to Final Release, andbeta update keeps showing in AppStore