SQLWarningDatabase ErrorJune 30, 2026

Database Error

SQLSTATE[HY000]: General error: 1216 Cannot delete or update a parent row: a foreign key constraint fails ('database_name'.'table_name'.'column_name' = 'related_table_name'.'related_column_name')

What This Error Means

This error occurs when attempting to delete or update a record in a table that has a foreign key relationship with another table. The deletion or update operation would break the relationship, violating the foreign key constraint.

Why It Happens

This error happens when there are records in the related table that reference the record being deleted or updated in the primary table. To resolve this, you need to either delete or update the related records first or modify the foreign key constraint to allow the deletion or update operation.

How to Fix It

  1. 1To fix this error, you can try the following steps:
  2. 21. Identify the records in the related table that reference the record being deleted or updated.
  3. 32. Delete or update those records first to break the foreign key relationship.
  4. 43. Once the relationship is broken, you can safely delete or update the record in the primary table.
  5. 5Example:
  6. 6// Broken code
  7. 7DELETE FROM table1 WHERE id = 1;
  8. 8// Fixed code
  9. 9DELETE FROM table1 WHERE id = 1;
  10. 10DELETE FROM table2 WHERE foreign_key = (SELECT id FROM table1 WHERE id = 1);

Example Code Solution

❌ Before (problematic code)
SQL
DELETE FROM orders WHERE customer_id = 1;
✅ After (fixed code)
SQL
DELETE FROM orders WHERE customer_id = 1;
DELETE FROM customers WHERE id = (SELECT customer_id FROM orders WHERE customer_id = 1);

Fix for SQLSTATE[HY000]: General error: 1216 Cannot delete or update a parent row: a foreign key constraint fails ('database_name'.'table_name'.'column_name' = 'related_table_name'.'related_column_name')

Related SQL Errors

Related SQL Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error