1950
MariaDBERRORCriticalConstraintHIGH confidence
Cannot delete or update a parent row: a foreign key constraint fails
Production Risk
High — referential integrity is being enforced correctly; fix the data or schema.
What this means
A DELETE or UPDATE on a parent table was blocked because a matching row exists in a child table with a FOREIGN KEY constraint that does not specify ON DELETE/UPDATE CASCADE or SET NULL.
Why it happens
- 1Deleting a parent row that is still referenced by one or more child rows.
- 2Updating the primary key of a parent row without cascading the change to child rows.
How to reproduce
trigger — this will error
trigger — this will error
DELETE FROM customers WHERE id = 1; -- child table orders still references id=1
expected output
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (mydb.orders, CONSTRAINT fk_cust FOREIGN KEY (customer_id) REFERENCES customers (id))
Fix 1
Delete or reassign child rows first
Delete or reassign child rows first
DELETE FROM orders WHERE customer_id = 1; DELETE FROM customers WHERE id = 1;
Why this works
Remove dependent child records before deleting the parent.
Fix 2
Use ON DELETE CASCADE on the foreign key
Use ON DELETE CASCADE on the foreign key
ALTER TABLE orders DROP FOREIGN KEY fk_cust, ADD CONSTRAINT fk_cust FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE;
Why this works
CASCADE automatically removes child rows when the parent is deleted.
What not to do
✕
Version notes
Sources
Official documentation ↗
MySQL 8.0 — 1950 ER_ROW_IS_REFERENCED_2
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev