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的具体含义,一开始并没有理解“值不匹配”的含义,只是直觉性地去检查了一下目标字段在两个表中的值,发现A表中有一行数据该字段的值在B表中已经删除了,这就导致了所谓的“值不匹配”的问题,然后手动删除了A表中的该条废弃数据,再次设置外键就成功了。

最后贴一下参考的原文,原文链接:https://blog.csdn.net/weixin_43713105/article/details/89396870
报错的原因大概分为三种:
原因一:
添加的外键列与另一个表的唯一索引列(一般是主键)的数据类型不同
原因二:
要添加外键的表类型与另一个表的存储引擎是不是都为innodb引擎
#查看表引擎
法一: show create table 表名;
法二:show table status from 数据库 where name=‘表名’;
法三:use information_schema;
select table_catalog,table_schema,table_name,engine from tables
where table_schema=‘数据库名’ and table_name=‘表名’;
原因三:
设置的外键与另一个表中的唯一索引列(一般是主键)中的值不匹配
#解决办法:删除要成为外键的列,再次创建并默认为NULL
MySQL设置外键报错 #1452 - Cannot add or update a child row: a foreign key constraint fails 解决方法的更多相关文章
- MySql数据库插入或更新报错:Cannot add or update a child row: a foreign key constraint fails
具体报错信息: Cannot add or update a child row: a foreign key constraint fails (`xxx`.`AAA`, CONSTRAINT `t ...
- Mysql数据库报错:Cannot add or update a child row: a foreign key constraint fails(添加多对多关系)
#创建班级表 class Classes(models.Model): title = models.CharField(max_length=32) n=models.ManyToManyField ...
- IntegrityError at /admin/users/userprofile/add/ (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_
报错现象 在执行 django 后台管理的时候添加数据导致 1452 错误 报错代码 IntegrityError at /admin/users/userprofile/add/ (1452, 'C ...
- Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails
错误: Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails 错误产生情景:我向一张带外键 ...
- admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l
在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...
- hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails
一篇文章里边有多张图片,典型的单向一对多关系 多方 当程序运行到这一句的时候必然报错 但是参考书也是这样写的 其中em是 EntityManager em = JPA.createEntityMana ...
- Mysql之1451 - Cannot delete or update a parent row: a foreign key constraint fails...解决办法记录
今天使用delete语句删除一张表中的一条信息时,提示了这么一个错误:1451 - Cannot delete or update a parent row: a foreign key constr ...
- ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint......
今天用Hibernate建立外键的时候发现没有建立 但是创建了这个字段 情景: user表有一字段role,role是role表id字段的外键 原因: user表中已经有记录了,而且有的记录role这 ...
- 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.所以,不太可能出现对于sa ...
随机推荐
- Nginx笔记总结六:Nginx location配置
语法规则:location [= | ~ | ~* | ^~] /uri/ {....} = 表示精确匹配 ^~ 表示uri以某个常规字符串开头 ~ 表示区分大小写的正则表达式 ~* 表示不区分大小写 ...
- 跨域带cookie失效的解决方案
在webpack的tableproxy那儿配置完跨域以后,想给cookie添加domain以便请求的时候带上cookie domain为localhost,cookie不会失效,但是一但改成baidu ...
- JS去重函数的扩展应用
数据: 账单id[1,1,1,1,2,2,2,3,3,3,3,3,3,3], 相对于账单id的金额[100,120,110,150,200,180,220,115,150,120,180,220,14 ...
- (四)mybatis缓存、事务、插件的基本知识
mybatis缓存.事务.插件的基础 一.缓存 (一)一级缓存与二级缓存 一级缓存 为了获得更好的性能,最重要的就是一级缓存.每个session对象维持一个一级缓存,session对象创建时缓存创建, ...
- JavaScript对象的几种创建方式与优缺点
JavaScript中常见的几种创建对象的方式有:Object构造函数模式.对象字面量模式.工厂模式.自定义构造函数模式.构造函数加原型组合模式:他们各自有各自的优缺点和使用场景. 1. Object ...
- Python包管理工具setuptools相关
setup函数常用参数: --name 包名称 --version 包版本 --author ...
- android应用程序源码结构分析
工程; 1. src文件夹存放源码. 2. gen下有跟src中一样的包文件,内部有一个名为R.java类,它是自动生成的一个类:该目录不用我们开发人员维护, 但又非常重要的目录 . 该目录用来存放由 ...
- c#或者C#.net中的“ToolTip”是“System.Windows.Forms.ToolTip”和“DevComponents.DotNetBar.ToolTip”之间的不明确的引用
“ToolTip”是“System.Windows.Forms.ToolTip”和“DevComponents.DotNetBar.ToolTip”之间的不明确的引用 ,在编程时,有时候会编译出现不明 ...
- 初识Flask框架
Flask简介: Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其 ...
- Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】
Java程序员常用的@Component.@Repository.@Controller.@Service系列[案例demo3] 很多程序员通过在类上使用@Repository.@Componen ...