MySQL error 1216: Cannot add or update a child row
The older form of the foreign key failure on insert or update, without the constraint details. Current servers report 1452 instead.
- Error number
1216- Name
ER_NO_REFERENCED_ROW- SQLSTATE
23000- Category
- Keys and constraints
What it means
1216 has the same meaning as 1452: the row you are writing references a parent row that doesn't exist. The difference is only that the message doesn't include the constraint definition. You may still see it from older servers or some code paths.
Common causes
- Inserting or updating a child row whose parent doesn't exist.
How to fix it
-
Follow the steps for error 1452: insert the parent first, or fix the foreign key value.
-
Use
SHOW ENGINE INNODB STATUSto see the latest foreign key error in detail.SHOW ENGINE INNODB STATUS;
Related errors
More keys and constraints errors
1062Duplicate entry for key1061Duplicate key name1451Foreign key fails: cannot delete parent row1215Cannot add foreign key constraint3780Foreign key columns are incompatible1822Missing index for foreign key constraint1824Failed to open the referenced table1048Column cannot be null
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.