Skip to main content

Cannot add or update child row

❌ Error

Cannot add or update a child row: a foreign key constraint fails

📍 Context

Happens when inserting into a table with a foreign key that references another table (e.g. licenses linked to panels, warehouses linked to owners), but the referenced row doesn’t exist.

✅ Solution

  • Insert the parent row first (e.g. create the warehouse before assigning it).
  • Or disable strict checks if you don’t need FK enforcement:
    SET FOREIGN_KEY_CHECKS=0;

ℹ️ Additional Information

  • Not all scripts use foreign keys — many comment them out to avoid issues.
  • If you enable them, always respect insert order.
  • For more on foreign keys, see the MySQL documentation.