Foreign key problems caused by rename in MySQL table

Background:
Due to the slow graphical display of the problems of zabbix monitoring, the events table of zabbix library is cleaned up. The original table rename is used as bak table in the cleaning process. After rebuilding the events table, part of the data in the backup table is imported into the new table.

After that, it was found that the zabbix platform was unable to alarm and recover the alarm. The log was similar to an error
 2315:20190301:104933.609 [Z3005] query failed: [1452] Cannot add or update a child row: a foreign key constraint fails (`zabbix`.`event_recovery`, CONSTRAINT `c_event_recovery_1` FOREIGN KEY (`eventid`) REFERENCES `events_bak20190225` (`eventid`) ON DELETE CASCADE) [insert into event_recovery (eventid,r_eventid,correlationid,c_eventid,userid) values (4242559,4242581,null,null,null),(4242561,4242580,null,null,null),(4242447,4242580,null,null,null);

After the original rename, the constraints of the associated foreign key table are all modified to the association of the archive table.
The treatment method is as follows:
View which tables are associated with archive tables when establishing foreign keys

select  distinct TABLE_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_SCHEMA  ='zabbix' and CONSTRAINT_name != 'PRIMARY' and REFERENCED_TABLE_NAME like 'event%';
+----------------+--------------------+-----------------------+
| TABLE_NAME     | CONSTRAINT_NAME    | REFERENCED_TABLE_NAME |
+----------------+--------------------+-----------------------+
| acknowledges   | c_acknowledges_2   | events                |
| alerts         | c_alerts_2         | events                |
| alerts         | c_alerts_5         | events                |
| event_recovery | c_event_recovery_1 | events                |
| event_recovery | c_event_recovery_2 | events                |
| event_recovery | c_event_recovery_3 | events                |
| event_tag      | c_event_tag_1      | events_bak20190225    |
| problem        | c_problem_1        | events                |
| problem        | c_problem_2        | events                |
+----------------+--------------------+-----------------------+

View table structure:

show create table event_tag\G
*************************** 1. row ***************************
       Table: event_tag
Create Table: CREATE TABLE `event_tag` (
  `eventtagid` bigint(20) unsigned NOT NULL,
  `eventid` bigint(20) unsigned NOT NULL,
  `tag` varchar(255) NOT NULL DEFAULT '',
  `value` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`eventtagid`),
  KEY `event_tag_1` (`eventid`),
  CONSTRAINT `c_event_tag_1` FOREIGN KEY (`eventid`) REFERENCES `events_bak20190225` (`eventid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

To modify a foreign key constraint:

alter table event_tag drop FOREIGN KEY  c_event_tag_1;
alter table event_tag add FOREIGN KEY  c_event_tag_1 (`eventid`) REFERENCES `events`(`eventid`) ON DELETE CASCADE;

zabbix has many foreign keys. Next time you clean up the data, delete it directly.

Keywords: MySQL Zabbix

Added by Francois on Sat, 30 Nov 2019 18:29:35 +0200