Category Archives: MySQL

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

Ever got an error like the one below?
Cannot add or update a child row: a foreign key constraint fails (`db01`.<result 2 when explaining filename '#sql-651_3'>, CONSTRAINT `table01_batch_id_foreign` FOREIGN KEY (`batch_id`) REFERENCES `the_batches` (`id`))

Then, there must be some rows with a column value (for batch_id) that is non-existent in the master table. In my case the column (in the child table) was set to DEFAULT value of 0 and obviously a matching id was not found in the master table.