ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails

MySQL添加外键失败  error 1452解决办法

举例:

两个表       用户表:user   班级表:  grade

用户表中每个用户对应一个班级ID,即gradeId

即:user表的主关键字gradeId是grade表中的Id

user表称为主表   grade表称为从表

【sql】

alter table user add foreign key (gradeId) references grade(Id );

【error】

【原因】

user表中的有的gradeId不属于grade表中的id

【解决方法】

修改数据,使user表中的gradeId全部属于grade表中的id

时间: 2024-08-02 15:11:59

ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails的相关文章

MySQL设置外键报错 #1452 - Cannot add or update a child row: a foreign key constraint fails 解决方法

MySQL数据库,当我尝试在A表中设置B表的主键为外键时,报出错误:#1452 - Cannot add or update a child row: a foreign key constraint fails  于是去搜索了一下 "#1452 - Cannot add or update a child row: a foreign key constraint fails" 的含义与解决方法,根据这篇博文去排查了报错原因,排除了可能1和可能2,然后去研究可能3的具体含义,一开始并

hibernate错误:Cannot add or update a child row: a foreign key constraint fails

今天对已存在的两个表中的一个表添加另一个表的主键为外键,遇到以下错误: sql 1452 Cannot add or update a child row:a foreign key constraint fails 原因: 设置的外键和对应的另一个表的主键值不匹配. 解决方法: 找出不匹配的值修改. 或者清空两表数据.

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

1.错误描述 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.Nat

insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.`productstatusrecord`, CONSTRAINT `p_cu` FOREIGN KEY (`cid`) REFERENCES `customer` (`cid`))错误

mybatis在insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.`productstatusrecord`, CONSTRAINT `p_cu` FOREIGN KEY (`cid`) REFERENCES `customer` (`cid`))错误,重点在"a foreign key constraint fails",说明主表和附表有外键关联,无法插

数据库添加数据报错:Cannot add or update a child row: a foreign key constraint fails

在更新表数据时出现了以下报错信息:Cannot add or update a child row: a foreign key constraint fails (o2o.tb_shop, CONSTRAINT fk_shop_profile FOREIGN KEY (owner_id) REFERENCES tb_person_info (user_id)) 也就是说tb_shop表中的涉及到的外键是fk_shop_profile,表tb_shop中的列owner_id和表tb_person

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

一篇文章里边有多张图片,典型的单向一对多关系 多方 当程序运行到这一句的时候必然报错 但是参考书也是这样写的 其中em是 EntityManager em = JPA.createEntityManager(); 我本就是为了省事儿,采用的hibernate,结果你又让我用JPA这不是扯淡吗???/ 经过两天的无脑研究 我发现一对多的情况,如果一方来控制,那么多方不能在实体中创建外键 也就是说,要这样做 /* * To change this license header, choose Lic

Cannot add or update a child row a foreign key constraint f

1.错误描述 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.Nat

MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sas_basic的操作能复制到该从库上. 2. 整个架构是1主2从,且都是选择性复制,上面这个从库是直接复制card,upay,deal,monitor,collect这几个数据库的数据,而另外一个从库则是忽略上述库,如下所示: 怀疑是在上述schema下,执行了DROP TABLE IF EXIST

MySQL:ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

MySQL在删除一张表时出现 ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails 可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHECKS = 0; 然后就可以删除表了. 删除完成后设置 SET FOREIGN_KEY_CHE