MySQL & MariaDB error 1451: Cannot delete or update a parent row (foreign key constraint fails)
Cannot delete or update a parent row: a foreign key constraint fails (`t_0`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`)) (5.6.51) · Cannot delete or update a parent row: a foreign key constraint fails (`t_0`.`emp`, CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dept_id`) REFERENCES `dept` (`id`)) (10.6.28) You tried to delete or change a parent row that child rows still reference, and the foreign key is declared without ON DELETE / ON UPDATE CASCADE or SET NULL.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_ROW_IS_REFERENCED_2 | ER_ROW_IS_REFERENCED_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 default foreign key action is RESTRICT / NO ACTION: a parent row can’t be removed, and its key can’t change, while children point at it. The message names the child table and constraint, so you know where the references are.
Why it happens
- Deleting a customer, department or product that still has orders, employees or order lines.
- Updating a primary key value that child rows reference.
TRUNCATEof a referenced table is refused even when no child rows exist. That case is error 1701, not 1451, on every version we tested.DROP TABLEof a referenced parent gives 3730 on MySQL 8.0 (1217 on older versions).
How to fix it
- Delete or re-point the child rows first, then the parent, in one transaction.
- If children should disappear with the parent, declare it:
FOREIGN KEY (dept_id) REFERENCES dept(id) ON DELETE CASCADE(orON DELETE SET NULLto keep them unassigned). That needs anALTER TABLE … DROP FOREIGN KEY …, ADD FOREIGN KEY …. - Find what references a table:
SELECT table_name, constraint_name FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE referenced_table_name = 'dept'; - To empty a referenced table, use
DELETEinstead ofTRUNCATE, or disableforeign_key_checksfor the session if you are deliberately reloading everything.
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); INSERT INTO emp VALUES (10, 1);Run:
DELETE FROM dept WHERE id = 1;
You get: Cannot delete or update a parent 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
Delete (or re-point) the children first, then the parent
DELETE FROM emp WHERE dept_id = 1; DELETE FROM dept WHERE id = 1; SELECT COUNT(*) FROM dept;Or declare the foreign key ON DELETE CASCADE / SET NULL
CREATE TABLE d2 (id INT PRIMARY KEY) ENGINE=InnoDB; CREATE TABLE e2 (id INT, dept_id INT, FOREIGN KEY (dept_id) REFERENCES d2(id) ON DELETE CASCADE) ENGINE=InnoDB; INSERT INTO d2 VALUES (1); INSERT INTO e2 VALUES (1, 1); DELETE FROM d2 WHERE id = 1; SELECT COUNT(*) FROM e2;
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Delete a parent row that child rows still reference repro | ✓ 1451 | ✓ 1451 | ✓ 1451 | ✓ 1451 | ✓ 1451 | ✓ 1451 | ✓ 1451 | ✓ 1451 | ✓ 1451 |
| TRUNCATE a referenced parent table version check | ✓ 1701 | ✓ 1701 | ✓ 1701 | ✓ 1701 | ✓ 1701 | ✓ 1701 | ✓ 1701 | ✓ 1701 | ✓ 1701 |
| Delete (or re-point) the children first, then the parent fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| Or declare the foreign key ON DELETE CASCADE / SET NULL fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
How the message changed between versions
| MySQL from | Symbol | Message |
|---|---|---|
| 5.0.96 | ER_ROW_IS_REFERENCED_2 | Cannot delete or update a parent row: a foreign key constraint fails (%.192s) |
| 8.4.0 | ER_ROW_IS_REFERENCED_2 | Cannot delete or update a parent row: a foreign key constraint fails%.192s |