Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails

Problem Description

Today, when inserting records into a table in the MySQL database, an error was reported. The complete error message description is as follows:

0	8	15:22:07	INSERT INTO `cuinn_menu` VALUES ('3', '2021-06-14 15:57:24', '2021-08-08 19:37:44', ' mdi-menu', '菜单管理', '1', '/menu/list', '10', '\0', '')	Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`db_boot_base`.`cuinn_menu`, CONSTRAINT `FKitpl2u3dbuubknu7p41mbiv05` FOREIGN KEY (`parent_id`) REFERENCES `cuinn_menu` (`id`))	0.015 sec

My table is a menu table with a structure like this:
Insert image description here


Cause Analysis:

This error message means that when I inserted data into the cuinn_menu table, I violated the foreign key constraint FKitpl2u3dbuubknu7p41mbiv05. Because the foreign key of my table is parent_id, which is also the primary key id of the table itself. When you see this insert statement, you can see that the id is 3 and the parent_id is 10, but there is no record with the id of 10 in this table. , therefore, we cannot insert this record into the table


solution:

From the above description, I know that before I create a submenu, it must have a parent menu. All my inserted sql is as follows:

INSERT INTO `cuinn_menu` VALUES ('3', '2021-06-14 15:57:24', '2021-08-08 19:37:44', ' mdi-menu', '菜单管理', '1', '/menu/list', '10', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('4', '2021-06-14 15:58:40', '2021-08-09 21:01:47', ' mdi-plus', '新增', '2', '/menu/add', '3', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('8', '2021-06-17 21:10:41', '2021-08-09 20:40:05', ' mdi-eyedropper-variant', '编辑', '3', 'edit(\'/menu/edit\')', '3', '', '');
INSERT INTO `cuinn_menu` VALUES ('9', '2021-06-17 21:11:13', '2021-08-09 20:59:04', ' mdi-window-close', '删除', '4', 'del(\'/menu/delete\')', '3', '', '');
INSERT INTO `cuinn_menu` VALUES ('10', '2021-06-17 21:34:18', '2021-06-17 21:34:18', ' mdi-settings', '系统设置', '0', '', null, '\0', '');
INSERT INTO `cuinn_menu` VALUES ('11', '2021-06-17 21:37:04', '2021-06-17 21:37:04', ' mdi-account-settings-variant', '角色管理', '5', '/role/list', '10', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('13', '2021-06-17 21:40:58', '2021-08-09 21:02:02', ' mdi-account-plus', '新增', '6', '/role/add', '11', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('15', '2021-08-08 19:38:34', '2021-08-08 19:38:34', ' mdi-account', '用户管理', '1', '/user/list', '10', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('16', '2021-08-08 19:39:30', '2021-08-09 21:02:12', ' mdi-account-plus', '新增', '2', '/user/add', '15', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('17', '2021-08-08 19:40:44', '2021-08-09 21:30:45', ' mdi-account-edit', '编辑', '3', 'edit(\'/user/edit\')', '15', '', '');
INSERT INTO `cuinn_menu` VALUES ('18', '2021-08-08 19:41:08', '2021-08-09 20:59:27', ' mdi-account-remove', '删除', '4', 'del(\'/user/delete\')', '15', '', '');
INSERT INTO `cuinn_menu` VALUES ('20', '2021-08-09 21:21:47', '2021-08-09 21:21:47', ' mdi-account-edit', '编辑', '7', 'edit(\'/role/edit\')', '11', '', '');
INSERT INTO `cuinn_menu` VALUES ('21', '2021-08-09 21:23:40', '2021-08-09 21:23:40', ' mdi-account-remove', '删除', '8', 'del(\'/role/delete\')', '11', '', '');
INSERT INTO `cuinn_menu` VALUES ('22', '2021-08-10 21:31:05', '2021-08-10 21:33:21', ' mdi-arrow-up-bold-circle', '上传图片', '0', '/upload/upload_photo', '15', '\0', '\0');
INSERT INTO `cuinn_menu` VALUES ('23', '2021-08-12 19:51:26', '2021-08-12 19:53:33', ' mdi-tag', '日志管理', '0', '/system/operator_log_list', '10', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('24', '2021-08-12 19:54:31', '2021-08-12 19:54:31', ' mdi-tag-remove', '删除', '0', 'del(\'/system/delete_operator_log\')', '23', '', '');
INSERT INTO `cuinn_menu` VALUES ('25', '2021-08-12 19:56:44', '2021-08-12 20:32:45', ' mdi-delete-circle', '清空日志', '0', 'delAll(\'/system/delete_all_operator_log\')', '23', '', '');
INSERT INTO `cuinn_menu` VALUES ('26', '2021-08-12 21:10:59', '2021-08-12 21:10:59', ' mdi-database', '数据备份', '0', '/database_bak/list', '10', '\0', '');
INSERT INTO `cuinn_menu` VALUES ('27', '2021-08-12 21:12:53', '2021-08-13 21:27:30', ' mdi-database-plus', '备份', '0', 'add(\'/database_bak/add\')', '26', '', '');
INSERT INTO `cuinn_menu` VALUES ('28', '2021-08-12 21:14:21', '2021-08-12 21:14:21', ' mdi-database-minus', '删除', '0', 'del(\'/database_bak/delete\')', '26', '', '');
INSERT INTO `cuinn_menu` VALUES ('29', '2021-08-14 10:56:57', '2021-08-14 10:56:57', ' mdi-database-minus', '还原', '0', 'restore(\'/database_bak/restore\')', '26', '', '');

Therefore, I need to sort these records before executing them, so that the parent menu records are executed first, that is, the records whose parent_id is null are executed first, then there will be no problem and this problem will be solved.

Guess you like

Origin blog.csdn.net/qq_40187702/article/details/130669480