mysqldude
Error codes › 1452

MySQL & MariaDB error 1452: Cannot add or update a child row (foreign key constraint fails)

MySQLCannot add or update a child row: a foreign key constraint fails…
MariaDBCannot add or update a child row: a foreign key constraint fails (…)
Example from our test runs: 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.

MySQLMariaDB
SymbolER_NO_REFERENCED_ROW_2ER_NO_REFERENCED_ROW_2
SQLSTATE2300023000
TypeServer errorServer error
Versionsevery release since 5.0 activeevery 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 NULL in 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 = 0 for 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.

  1. Connect with mysql -u root -p and 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);
  2. 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

  1. 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;
  2. 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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
✓✓✓✓✓✓✓✓✓
Insert a child row whose parent doesn't exist repro
MySQL 5.6✓ 14525.7✓ 14528.0✓ 14528.4✓ 14529✓ 1452
MariaDB 10.6✓ 145210.11✓ 145211.4✓ 145211.8✓ 1452
Insert the parent first (or use NULL if the relationship is optional) fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Find orphan rows before adding a foreign key to existing data fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

Related errors

  • 1451 ER_ROW_IS_REFERENCED_2explained
  • 1215 ER_CANNOT_ADD_FOREIGNexplained
  • 1216 ER_NO_REFERENCED_ROW

Where this error shows up in our guides

How the message changed between versions
MySQL fromSymbolMessage
5.0.96ER_NO_REFERENCED_ROW_2Cannot add or update a child row: a foreign key constraint fails (%.192s)
8.4.0ER_NO_REFERENCED_ROW_2Cannot add or update a child row: a foreign key constraint fails%.192s