MySQL & MariaDB error 1452: Cannot add or update a child row (foreign key constraint fails)
Cannot add or update a child row: a foreign key constraint fails (`t_0`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`deptid`) REFERENCES `depts` (`dep (5.6.51) · Cannot add or update a child row: a foreign key constraint fails (`t_0`.`employee`, CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`deptid`) REFERENCES `depts` (`dep (10.6.28) You inserted or updated a row whose foreign key value has no matching row in the parent table. InnoDB refuses it to keep the relationship consistent.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_NO_REFERENCED_ROW_2 | ER_NO_REFERENCED_ROW_2 |
| SQLSTATE | 23000 | 23000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
The message spells out the constraint: Cannot add or update a child row: a foreign key constraint fails (`shop`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`)). The child table is emp, the column dept_id, and the value you used doesn’t exist in dept.id.
Why it happens
- The parent row really doesn’t exist: a wrong id, or it was deleted.
- Rows are inserted in the wrong order (children before parents), common in imports and test fixtures.
- Adding a foreign key to a table that already contains orphan values.
- The values look equal but aren’t: different types, signedness, trailing spaces, or character sets / collations between parent and child columns.
How to fix it
- Insert the parent first, or use
NULLin the child if the relationship is optional (NULL never violates a foreign key). - Before adding a constraint to existing data, find the orphans:
SELECT c.* FROM child c LEFT JOIN parent p ON p.id = c.parent_id WHERE c.parent_id IS NOT NULL AND p.id IS NULL; - For bulk loads in a known-good order you can
SET foreign_key_checks = 0for the session, but rows loaded that way are never checked afterwards, so verify them with the query above.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE dept (id INT PRIMARY KEY) ENGINE=InnoDB; CREATE TABLE emp (id INT PRIMARY KEY, dept_id INT, FOREIGN KEY (dept_id) REFERENCES dept(id)) ENGINE=InnoDB; INSERT INTO dept VALUES (1);Run:
INSERT INTO emp VALUES (10, 99);
You get: Cannot add or update a child row: a foreign key constraint fails (`t_0`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`))(11.8.9)
Verified fixes
Insert the parent first (or use NULL if the relationship is optional)
INSERT INTO dept VALUES (99); INSERT INTO emp VALUES (10, 99); INSERT INTO emp VALUES (11, NULL); SELECT COUNT(*) FROM emp;Find orphan rows before adding a foreign key to existing data
CREATE TABLE emp2 (id INT, dept_id INT); INSERT INTO emp2 VALUES (1, 1), (2, 42); SELECT e.id, e.dept_id FROM emp2 e LEFT JOIN dept d ON d.id = e.dept_id WHERE e.dept_id IS NOT NULL AND d.id IS NULL;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Insert a child row whose parent doesn't exist repro | ✓ 1452 | ✓ 1452 | ✓ 1452 | ✓ 1452 | ✓ 1452 | ✓ 1452 | ✓ 1452 | ✓ 1452 | ✓ 1452 |
| Insert the parent first (or use NULL if the relationship is optional) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Find orphan rows before adding a foreign key to existing data fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
Where this error shows up in our guides
- Foreign key constraints impact · INSERT with a missing parent (expect 1452, original) MySQL 5.6 5.7 8.0 8.4 9MariaDB 10.6 10.11 11.4 11.8
How the message changed between versions
| MySQL from | Symbol | Message |
|---|---|---|
| 5.0.96 | ER_NO_REFERENCED_ROW_2 | Cannot add or update a child row: a foreign key constraint fails (%.192s) |
| 8.4.0 | ER_NO_REFERENCED_ROW_2 | Cannot add or update a child row: a foreign key constraint fails%.192s |