mysqldude
Error codes › 1451

MySQL & MariaDB error 1451: Cannot delete or update a parent row (foreign key constraint fails)

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

MySQLMariaDB
SymbolER_ROW_IS_REFERENCED_2ER_ROW_IS_REFERENCED_2
SQLSTATE2300023000
TypeServer errorServer error
Versionsevery release since 5.0 activeevery 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.
  • TRUNCATE of a referenced table is refused even when no child rows exist. That case is error 1701, not 1451, on every version we tested.
  • DROP TABLE of 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 (or ON DELETE SET NULL to keep them unassigned). That needs an ALTER 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 DELETE instead of TRUNCATE, or disable foreign_key_checks for 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.

  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);
    INSERT INTO emp VALUES (10, 1);
  2. 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

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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
✓✓✓✓✓✓✓✓✓
Delete a parent row that child rows still reference repro
MySQL 5.6✓ 14515.7✓ 14518.0✓ 14518.4✓ 14519✓ 1451
MariaDB 10.6✓ 145110.11✓ 145111.4✓ 145111.8✓ 1451
TRUNCATE a referenced parent table version check
MySQL 5.6✓ 17015.7✓ 17018.0✓ 17018.4✓ 17019✓ 1701
MariaDB 10.6✓ 170110.11✓ 170111.4✓ 170111.8✓ 1701
Delete (or re-point) the children first, then the parent fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Or declare the foreign key ON DELETE CASCADE / SET NULL 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

  • 1452 ER_NO_REFERENCED_ROW_2explained
  • 1701 ER_TRUNCATE_ILLEGAL_FK
  • 1217 ER_ROW_IS_REFERENCED
How the message changed between versions
MySQL fromSymbolMessage
5.0.96ER_ROW_IS_REFERENCED_2Cannot delete or update a parent row: a foreign key constraint fails (%.192s)
8.4.0ER_ROW_IS_REFERENCED_2Cannot delete or update a parent row: a foreign key constraint fails%.192s