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 ...
随机推荐
- PHP常用接口数据过滤的方法
<?php /** * global.func.php 公共函数库 */ /** * 返回经addslashes处理过的字符串或数组 * @param $string 需要处理的字符串或数组 * ...
- 关于log4j中log4j.properties和log4j.xml的加载顺序
如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解. log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找log4j.properti ...
- 引入 JPEGCodec;JPEGImageEncoder; 图片处理;MyEclipse编译时报错处理
在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JP ...
- figure设置坐标轴
import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) y1=x*2+1 y2=x**2 plt.plot( ...
- songCMS 3.15 cookie SQLINJ
./code/profile.php ... $db = new db(); $SQL = "SELECT * FROM `{$dbprefix}user` WHERE `ID` = {$_ ...
- 《ASP.NET Core 高性能系列》Span<T>和Memory<T>
一.Span<T>概述 原文:Provides a type- and memory-safe representation of a contiguous region of arbit ...
- LeetCode 题解 | 1. 两数之和
题目描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], t ...
- 分割文件和数据(linux)
在某些情况下,必须把文件分割成多个更小的片段.如今我们分割文件的目的比如:提高可读性.生成日志.通过Email发送文件等等. 假设我们有一个erro.log的测试文件,其大小为14M.你可以将该文件分 ...
- python 生成随机数、生成 uuid
1. 使用 uuid.uuid1 产生一个随机数 2. 在使用 random.sample() 产生一个随机字符串 3. 将两者进行拼接 import uuid import random def r ...
- Javascript学习笔记-基本概念-语法、关键字和保留字、变量
语法 1.区分大小写 2.标识符 所谓标识符,就是指变量.函数.属性的名字,或者函数的参数. 命名规则: 第一个字符必须是一个字母.下划线(_)或一个美元符号($): 其他字符可以是字母.下划线.美元 ...