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 ...
随机推荐
- 九:SpringBoot-整合Mybatis框架,集成分页助手插件
九:SpringBoot-整合Mybatis框架,集成分页助手插件 1.Mybatis框架 1.1 mybatis特点 1.2 适用场景 2.SpringBoot整合MyBatis 2.1 核心依赖 ...
- Tomcat Servlet工作原理
前言 Tomcat的启动过程 Web应用初始化 创建Servlet实例 初始化Servlet 执行service方法 前言 Servlet实际上就是一个java类,只不过可以和浏览器进行一些数据的交换 ...
- 面向对象编程(封装、封装的意义、封装与扩展性、@property)
1.封装之如何实现属性的隐藏 封装: __x=1 # 把数据属性隐藏 (如何实现隐藏) 类定义阶段 __开头发生了变形 __x --> _A__x特点: 1.在类外部无法直接:obj.__Att ...
- js文字颜色闪烁
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- DedeCMS程序使用拼音首字母做栏目名称的方法
Dedecms织梦程序默认使用拼音为保存目录的时候使用的是中文全拼,当遇到栏目名称比较长的时候目录名称看起来有点冗长,这时候大多数站长喜欢使用拼音首字母作为栏目的保存目录,那么就需要修改 dede/c ...
- C#脚本引擎RulesEngine
当编写应用程序时,经常性需要花费大量的时间与精力处理业务逻辑,往往业务逻辑的变化需要重构或者增加大量代码,对开发测试人员很不友好. 之前在这篇文章说过,可以使用脚本引擎来将我们需要经常变化的代码进行动 ...
- 2018 ccpc吉林 The Tower
传送门:HDU - 6559 题意 在一个三维空间,给定一个点和他的三维速度,给定一个圆锥,问这个点最早什么时候能撞上圆锥. 题解 本来一直想着怎么求圆锥的方程,然后....队友:这不是二分吗!然后问 ...
- HDU2732 Leapin' Lizards 最大流
题目 题意: t组输入,然后地图有n行m列,且n,m<=20.有一个最大跳跃距离d.后面输入一个n行的地图,每一个位置有一个值,代表这个位置的柱子可以经过多少个猴子.之后再输入一个地图'L'代表 ...
- Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)
题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...
- AC自动机算法 && 例题
参考链接: https://blog.csdn.net/bestsort/article/details/82947639#commentBox https://blog.csdn.net/niush ...