MySQL 8.4Lab-verified · 2026-12-01

MySQL ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

Rows in a child table still reference the parent row you are deleting or whose key you are changing.

MySQL 8.4.13
1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`lab_a`.`orders`, CONSTRAINT `fk_orders_customer` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`))

Jump to a cause

  1. 01Deleting a parent row that still has child rows
  2. 02Changing a parent key value that child rows reference
  3. 03Deleting header rows (orders) that have detail rows (order lines)
  4. 04ON DELETE CASCADE is set, but a grandchild table blocks it
  5. 05Deleting a manager row in a self-referencing table

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-12-01 · container image mysql:8.4

The message names the child table and its constraint, not the table you ran DELETE or UPDATE on: (lab_a.orders, CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id)). That child table still has rows pointing at the parent row.

MySQL 8.4 returns this message in English even when lc_messages is set to Korean or Japanese.

01Deleting a parent row that still has child rows

Customer 1 has two orders (10 and 11) in orders. The foreign key has no ON DELETE action, so MySQL refuses to delete the customer.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id));
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
INSERT INTO orders VALUES (10, 1), (11, 1);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
DELETE FROM customers WHERE id = 1;
✕ ERROR
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`lab_a`.`orders`, CONSTRAINT `fk_orders_customer` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`))
Checkrun as lab_a
SELECT id, customer_id FROM orders WHERE customer_id = 1;
OUTPUT
+----+-------------+
| id | customer_id |
+----+-------------+
| 10 |           1 |
| 11 |           1 |
+----+-------------+
2 rows in set (0.00 sec)
Fixrun as lab_a
DELETE FROM orders WHERE customer_id = 1;
OUTPUT
Query OK, 2 rows affected (0.00 sec)

Check the child rows first, then delete them (2 rows) and delete the parent. Customer 1 is gone and only Lee remains. If the child rows must be kept, move them to another parent instead of deleting them.

After the fixrun as lab_a
DELETE FROM customers WHERE id = 1;
SELECT * FROM customers;
✓ SUCCESS
Query OK, 1 row affected (0.01 sec)

+----+------+
| id | name |
+----+------+
|  2 | Lee  |
+----+------+
1 row in set (0.00 sec)

02Changing a parent key value that child rows reference

Employee 1 references department code SL. Renaming the code to SALES would leave the employee pointing at a value that no longer exists, so the UPDATE fails with the same 1451.

Setup
Setuprun as lab_a
CREATE TABLE depts (code VARCHAR(10) PRIMARY KEY, name VARCHAR(50));
CREATE TABLE emps (id INT PRIMARY KEY, dept_code VARCHAR(10), CONSTRAINT fk_emps_dept FOREIGN KEY (dept_code) REFERENCES depts (code));
INSERT INTO depts VALUES ('SL', 'Sales');
INSERT INTO emps VALUES (1, 'SL');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)
Reproducerun as lab_a
UPDATE depts SET code = 'SALES' WHERE code = 'SL';
✕ ERROR
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`lab_a`.`emps`, CONSTRAINT `fk_emps_dept` FOREIGN KEY (`dept_code`) REFERENCES `depts` (`code`))
Fixrun as lab_a
ALTER TABLE emps DROP FOREIGN KEY fk_emps_dept;
ALTER TABLE emps ADD CONSTRAINT fk_emps_dept FOREIGN KEY (dept_code) REFERENCES depts (code) ON UPDATE CASCADE;
OUTPUT
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

Recreate the foreign key with ON UPDATE CASCADE. The same UPDATE then succeeds and the employee's dept_code changes to SALES automatically.

After the fixrun as lab_a
UPDATE depts SET code = 'SALES' WHERE code = 'SL';
SELECT id, dept_code FROM emps;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

+----+-----------+
| id | dept_code |
+----+-----------+
|  1 | SALES     |
+----+-----------+
1 row in set (0.00 sec)

03Deleting header rows (orders) that have detail rows (order lines)

Deleting canceled orders fails because order 1 still has two lines in order_lines. This header/detail pattern is the most common place to hit 1451 in business systems.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, status VARCHAR(10));
CREATE TABLE order_lines (id INT PRIMARY KEY, order_id INT, item VARCHAR(20), CONSTRAINT fk_lines_order FOREIGN KEY (order_id) REFERENCES orders (id));
INSERT INTO orders VALUES (1, 'CANCELED'), (2, 'PAID');
INSERT INTO order_lines VALUES (1, 1, 'pen'), (2, 1, 'cup'), (3, 2, 'book');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
DELETE FROM orders WHERE status = 'CANCELED';
✕ ERROR
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`lab_a`.`order_lines`, CONSTRAINT `fk_lines_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`))
Fixrun as lab_a
ALTER TABLE order_lines DROP FOREIGN KEY fk_lines_order;
ALTER TABLE order_lines ADD CONSTRAINT fk_lines_order FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE;
OUTPUT
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

If the detail rows have no meaning without the header, recreate the foreign key with ON DELETE CASCADE. Deleting the canceled order then removes its two lines too; only line 3 of order 2 remains. Use cascade only where removing the children is really intended.

After the fixrun as lab_a
DELETE FROM orders WHERE status = 'CANCELED';
SELECT * FROM order_lines ORDER BY id;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)

+----+----------+------+
| id | order_id | item |
+----+----------+------+
|  3 |        2 | book |
+----+----------+------+
1 row in set (0.00 sec)

04ON DELETE CASCADE is set, but a grandchild table blocks it

orders → order_lines is ON DELETE CASCADE, so deleting an order should delete its lines. Yet deleting order 1 failed with 1451, and the table in the message is not a child of orders but shipments (constraint fk_ship_line).

When CASCADE tried to delete line 12, a shipment row referenced that line and blocked it. The confusing part is that the table in the message may not be a direct child of the table you deleted from.

Setup
Setuprun as lab_a
CREATE TABLE orders (id INT PRIMARY KEY, status VARCHAR(10));
CREATE TABLE order_lines (id INT PRIMARY KEY, order_id INT,
  CONSTRAINT fk_lines_order FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE);
CREATE TABLE shipments (id INT PRIMARY KEY, line_id INT,
  CONSTRAINT fk_ship_line FOREIGN KEY (line_id) REFERENCES order_lines (id));
INSERT INTO orders VALUES (1, 'CANCELED'), (2, 'PAID');
INSERT INTO order_lines VALUES (11, 1), (12, 1), (21, 2);
INSERT INTO shipments VALUES (100, 12);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

Query OK, 1 row affected (0.00 sec)
Reproducerun as lab_a
DELETE FROM orders WHERE id = 1;
✕ ERROR
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`lab_a`.`shipments`, CONSTRAINT `fk_ship_line` FOREIGN KEY (`line_id`) REFERENCES `order_lines` (`id`))
Check the causerun as lab_a
SELECT table_name, constraint_name, referenced_table_name, delete_rule
FROM information_schema.referential_constraints
WHERE constraint_schema = DATABASE()
ORDER BY table_name;
SELECT s.id AS shipment_id, s.line_id, l.order_id
FROM shipments s JOIN order_lines l ON l.id = s.line_id
WHERE l.order_id = 1;
OUTPUT
+-------------+-----------------+-----------------------+-------------+
| TABLE_NAME  | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | DELETE_RULE |
+-------------+-----------------+-----------------------+-------------+
| order_lines | fk_lines_order  | orders                | CASCADE     |
| shipments   | fk_ship_line    | order_lines           | NO ACTION   |
+-------------+-----------------+-----------------------+-------------+
2 rows in set (0.00 sec)

+-------------+---------+----------+
| shipment_id | line_id | order_id |
+-------------+---------+----------+
|         100 |      12 |        1 |
+-------------+---------+----------+
1 row in set (0.00 sec)

fk_lines_order is CASCADE, but fk_ship_line is NO ACTION. Shipment 100 references line 12 of order 1.

Fixrun as lab_a
DELETE s FROM shipments s JOIN order_lines l ON l.id = s.line_id WHERE l.order_id = 1;
OUTPUT
Query OK, 1 row affected (0.01 sec)

Deleting the shipment rows of order 1 first let the order DELETE succeed, and CASCADE also removed lines 11 and 12, leaving only line 21. If shipment history must be kept, consider changing the order status instead of deleting it.

After the fixrun as lab_a
DELETE FROM orders WHERE id = 1;
SELECT * FROM order_lines ORDER BY id;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)

+----+----------+
| id | order_id |
+----+----------+
| 21 |        2 |
+----+----------+
1 row in set (0.00 sec)

05Deleting a manager row in a self-referencing table

employees.manager_id references id in the same table. Deleting employee 1 (Kim) failed with 1451 because Lee and Park report to employee 1. In the message, both the child table and the referenced table are employees.

Setup
Setuprun as lab_a
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(20), manager_id INT,
  CONSTRAINT fk_emp_manager FOREIGN KEY (manager_id) REFERENCES employees (id));
INSERT INTO employees VALUES (1, 'Kim', NULL);
INSERT INTO employees VALUES (2, 'Lee', 1), (3, 'Park', 1);
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
DELETE FROM employees WHERE id = 1;
✕ ERROR
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`lab_a`.`employees`, CONSTRAINT `fk_emp_manager` FOREIGN KEY (`manager_id`) REFERENCES `employees` (`id`))
Check the causerun as lab_a
SELECT id, name, manager_id FROM employees WHERE manager_id = 1;
OUTPUT
+----+------+------------+
| id | name | manager_id |
+----+------+------------+
|  2 | Lee  |          1 |
|  3 | Park |          1 |
+----+------+------------+
2 rows in set (0.00 sec)

Two employees (Lee and Park) have manager_id = 1. Those rows reference the row being deleted.

Fixrun as lab_a
UPDATE employees SET manager_id = NULL WHERE manager_id = 1;
OUTPUT
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

After setting their manager_id to NULL (or to another manager), deleting employee 1 succeeded.

After the fixrun as lab_a
DELETE FROM employees WHERE id = 1;
SELECT * FROM employees ORDER BY id;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)

+----+------+------------+
| id | name | manager_id |
+----+------+------------+
|  2 | Lee  |       NULL |
|  3 | Park |       NULL |
+----+------+------------+
2 rows in set (0.00 sec)

FAQ

How do I fix MySQL error 1451 Cannot delete or update a parent row?

Look at the child table in the message, find the rows that reference the parent, and delete or reassign them first. Or recreate the foreign key with ON DELETE CASCADE if the children should go with the parent.

How do I delete a row with a foreign key constraint in MySQL?

Delete the child rows first, then the parent. To do it in one statement, the foreign key needs ON DELETE CASCADE.

Why does UPDATE give ERROR 1451?

You changed a parent key value that child rows still use. Recreate the foreign key with ON UPDATE CASCADE so the child values follow the change.

What is the difference between MySQL 1451 and 1452?

1451 is raised when changing or deleting the parent row. 1452 is raised when inserting or updating a child row whose value does not exist in the parent.

I set ON DELETE CASCADE but still get 1451. Why?

Read the table name in the message. It may be a grandchild, not a direct child of the table you deleted from. In the run above, shipments referenced order lines that CASCADE was about to delete. Check delete_rule in information_schema.referential_constraints for each level.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13