DBAtlas · Database operations atlas

1452: Cannot add or update a child row: a foreign key constraint fails

MySQL

What does it mean?

You are inserting or updating a child row, but the parent key value it points to does not exist in the referenced table; the foreign key constraint rejects it.

Why does it happen?

An INSERT/UPDATE referencing a non-existent parent key, data loaded in the wrong order (child first) or a deleted parent row.

How to fix it

Insert the parent row first, then the child row. Verify that the referenced value exists: SELECT * FROM parent_table WHERE key_column = value. Preserve parent-child order in bulk loads. Use SET FOREIGN_KEY_CHECKS=0 only in controlled maintenance operations and set it back to 1 when done.

Open in DBAtlas →

More from this engine