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
错误产生情景:我向一张带外键的表中插入一条新的数据
表情况:
表tb_user:
CREATE TABLE `tb_user` (
`uname` VARCHAR(30) NOT NULL COMMENT '用户名',
`upwd` VARCHAR(30) NOT NULL DEFAULT '000000' COMMENT '密码',
`email` VARCHAR(30) NOT NULL COMMENT '邮箱',
`phone` CHAR(11) NOT NULL COMMENT '电话(手机号)',
`role` INT(1) NOT NULL DEFAULT '0' COMMENT '角色(1表示管理员)',
PRIMARY KEY (`phone`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='用户信息表'
表tb_address:(关联表 tb_user)
CREATE TABLE `tb_address` (
`rid` INT(11) NOT NULL AUTO_INCREMENT COMMENT '记录编号',
`user_id` CHAR(11) NOT NULL COMMENT '用户编号(用户手机号)',
`address` VARCHAR(50) NOT NULL COMMENT '地址',
`added` DATETIME NOT NULL COMMENT '添加日期/时间',
`receiver` VARCHAR(30) NOT NULL COMMENT '收件人(姓名)',
`receiver_phone` CHAR(11) NOT NULL COMMENT '收件人电话',
PRIMARY KEY (`rid`),
KEY `fk_address_user` (`user_id`),
CONSTRAINT `fk_address_user` FOREIGN KEY (`user_id`) REFERENCES `tb_user` (`phone`)
) ENGINE=INNODB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 COMMENT='用户地址表'
错误产生:我执行以下语句时报错。
INSERT INTO tb_address (user_id,address,added,receiver,receiver_phone) VALUES('10086','彩云之南',CURRENT_TIMESTAMP,'范闲','4008208820')
分析:向tb_address 中插入了一条新数据,此数据中外键所在字段在表tb_user中并没有。注意:user_id与表tb_user的字段phone关联。
解决:先向 tb_user 表中插入数据此数据的 phone 字段与之后要向 tb_address 表中插入的数据的 user_id 字段一致
Error Code: 1452 Cannot add or update a child row: a foreign key constraint fails的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Error 'Cannot add or update a child row: a foreign key constraint fails故障解决
一大早的,某从库突然报出故障:SQL线程中断! 查看从库状态: mysql> show slave status\G Slave_IO_State: Waiting for master to ...
- java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.ConstraintV ...
- 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`.` ...
- 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 ...
- hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails
一篇文章里边有多张图片,典型的单向一对多关系 多方 当程序运行到这一句的时候必然报错 但是参考书也是这样写的 其中em是 EntityManager em = JPA.createEntityMana ...
- 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 ...
随机推荐
- REST以及RESTful
java作为一门后端语言,其厉害之处在于web,大家比较熟知的各种网络应用,java都能做,那么在这个移动优先的时代,如何继续发挥java的强大呢.通常是让java作为一个app的服务端,为app客户 ...
- Java——变量类型
Java变量类型: 在Java中,所有的变量在使用前必须声明.格式: type identifier [ = value ][, identifier [ =value]-.]; type为Java数 ...
- snmp协议 及snmpwalk
推荐阅读: snmp及工具:https://www.jianshu.com/p/dc2dc0222940 snmp协议详解:https://blog.csdn.net/shanzhizi/articl ...
- 调用个别f5 负载端口为80的vs时,返回值为空的问题
现状: vs负载端口为80并添加XFF,pool包含2个member,member的monitor端口为80&9000. 故障现象: 应用同事描述说再完全复制了一个member并添加到pool ...
- JavaWeb-tomcat安装(Unsupported major.minor version 51.0/startup.bat闪退)
JavaWeb-tomcat安装(Unsupported major.minor version 51.0) 一 启动startup.bat 出错i 今天安装tomcat出错,折腾了一下午,收获了许多 ...
- 在 .NET Core Logging中使用 Trace和TraceSource
本文介绍了在.NET Core中如何在组件设计中使用Trace和TraceSource. 在以下方面会提供一些帮助: 1.你已经为.NET Framework和.NET Core / .NET Sta ...
- Codeforces Round #651 (Div. 2) B. GCD Compression(数论)
题目链接:https://codeforces.com/contest/1370/problem/B 题意 给出 $2n$ 个数,选出 $2n - 2$ 个数,使得它们的 $gcd > 1$ . ...
- Relatives POJ - 2407 欧拉函数
题意: 给你一个正整数n,问你在区间[1,n)中有多少数与n互质 题解: 1既不是合数也不是质数(1不是素数) 互质是公约数只有1的两个整数,叫做互质整数.公约数只有1的两个自然数,叫做互质自然数 所 ...
- 二叉排序树的构造 && 二叉树的先序、中序、后序遍历 && 树的括号表示规则
二叉排序树的中序遍历就是按照关键字的从小到大顺序输出(先序和后序可没有这个顺序) 一.以序列 6 8 5 7 9 3构建二叉排序树: 二叉排序树就是中序遍历之后是有序的: 构造二叉排序树步骤如下: 插 ...
- centos 7下安装配置Supervisor
1.安装Supervisor centos下安装yum install supervisor 2. systemctl enable supervisord 开机自启 systemctl start ...