Cannot add or update a child row: a foreign key constraint fails
Production Risk
HIGH — inserts fail; indicates a logic error in insert ordering or data validation.
Error 1452 (SQLSTATE 23000) is the mirror of 1451. It is raised when an INSERT or UPDATE on a child table would create a row whose foreign key value does not match any row in the parent table.
- 1Inserting a child row with a foreign key value that does not exist in the parent table
- 2Updating the foreign key column to a value that does not exist in the parent
- 3Inserting rows out of order — child before parent — in a bulk load
- 4The parent row was deleted between the application reading the ID and performing the insert
Inserting an order for a non-existent customer.
INSERT INTO orders (customer_id, total) VALUES (9999, 150.00); -- Customer 9999 does not exist in customers table
expected output
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`mydb`.`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`))
Fix 1
Ensure the parent row exists before inserting the child
WHEN In normal application flow — create the parent first.
-- Insert customer first:
INSERT INTO customers (name) VALUES ('Maria Silva');
SET @customer_id = LAST_INSERT_ID();
-- Then insert the order:
INSERT INTO orders (customer_id, total) VALUES (@customer_id, 150.00);Why this works
LAST_INSERT_ID() returns the auto-increment ID of the most recently inserted row in the same session, ensuring the child references a valid parent.
Fix 2
Wrap parent and child inserts in a transaction
WHEN To ensure atomicity when inserting related rows.
START TRANSACTION;
INSERT INTO customers (name) VALUES ('Maria Silva');
INSERT INTO orders (customer_id, total) VALUES (LAST_INSERT_ID(), 150.00);
COMMIT;Why this works
Wrapping in a transaction ensures either both rows are inserted or neither is, preventing partial states.
✕ Use SET FOREIGN_KEY_CHECKS=0 to force inserts
Creates orphaned child rows with no parent, corrupting referential integrity silently.
MariaDB Server error code 1452 / ER_NO_REFERENCED_ROW_2
MariaDB Foreign Keys ↗MariaDB LAST_INSERT_ID ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev