Mysql – SQL Error 1452 : Cannot add or update a child row: a foreign key constraint fails

debuggingforeign keyMySQL

I am trying to install Zabbix by following this guide. I ran into the following problems when trying to run data.sql after running scheme.sql successfully

mysql -D zabbix -uzabbix -pPassword < /home/zabbix/zabbix-2.4.5/database/mysql/schema.sql

mysql -D zabbix -uzabbix -pPassword < /home/zabbix/zabbix-2.4.5/database/mysql/data.sql

ERROR 1452 (23000) at line 2989: Cannot add or update a child row: a
foreign key constraint fails ('zabbix'.'sysmaps_elements', CONSTRAINT
'c_sysmaps_elements_2' FOREIGN KEY ('iconid_off') REFERENCES 'images'
('imageid'))

I cannot figure out why I would not be able to add or update a child row , it looks perfectly fine to me.

The scripts can be downloaded from here

Best Answer

  1. check the order of sql execution. schema.sql --> images.sql --> data.sql

  2. Make sure the image data is actually entered. select * from images;

This is because images.sql is not executed because autocommit is not set in some DBs. This is because there is no "commit;" in the last sentence of images.sql. so add the statement "commit;" to images.sql.