Cannot delete or update a parent row: a foreign key constraint fails (`smartv2_2`.`sys_user_role_`,

1.删除表中某条数据,提示有外键约束。

mysql> mysql> delete from sys_user_ where user_name = 'qd_admin';

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`smart`.`sys_user_role_`, CONSTRAINT `FK7bl169sac5hfy5bn80w46af7f` FOREIGN KEY (`user_id`) REFERENCES `sys_user_` (`id`))

解决方法:

1.临时取消外键约束:

mysql> SET FOREIGN_KEY_CHECKS = 0;

Query OK, 0 rows affected (0.00 sec)

2.执行删除表记录命令

3.恢复外键约束:

mysql> SET FOREIGN_KEY_CHECKS = 1;

Query OK, 0 rows affected (0.00 sec)

结束。

原文地址:https://www.cnblogs.com/liusingbon/p/13964556.html