错误:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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`.` ...

  7. 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 ...

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

    一篇文章里边有多张图片,典型的单向一对多关系 多方 当程序运行到这一句的时候必然报错 但是参考书也是这样写的 其中em是 EntityManager em = JPA.createEntityMana ...

  9. 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 ...

随机推荐

  1. 高性能缓存 Caffeine 原理及实战

    一.简介 Caffeine 是基于Java 8 开发的.提供了近乎最佳命中率的高性能本地缓存组件,Spring5 开始不再支持 Guava Cache,改为使用 Caffeine. 下面是 Caffe ...

  2. Java——集合框架之Set&HashSet,HashMap,泛型,compareTo

    Set Set接口--数据存放无序,非常简单,主要呈现信息列表 Set接口存储一组唯一.无序的对象 HashSet是Set接口常用的实现类 Set接口不存在get方法 Iterator接口:表示对集合 ...

  3. Java服务端性能优化

    <Java程序性能优化>说性能优化包含五个层次:设计调优.代码调优.JVM调优.数据库调优.操作系统调优. 常用的几个代码优化方案: 使用单例 对于IO处理.数据库连接.配置文件解析加载等 ...

  4. java正则表示判断。是否以某个关键字结尾的

    String defaultPageSqlId = ".*ByPaging$"; System.out.println("dsdsdsdByPaging".ma ...

  5. 远程url文件地址转成byte

    public static byte[] urlTobyte(String url) throws MalformedURLException { URL ur = new URL(url); Buf ...

  6. 【STM32】无法下载程序

    错误: 使用keil MDK向STM32下载时出现各种错误 Internal command error.Error:Flash download failed.  Target DLL has be ...

  7. [The Preliminary Contest for ICPC Asia Nanjing 2019] L-Digit sum

    题意 $S_{b}(n)$表示数字$n$在$b$进制下各位的和,对于给定的数$N$和$b$,求出$\sum_{n=1}^{N}S_{b}(n)$ $[ link ]$ 分析 题解上写的是签到题,这是个 ...

  8. B. Modular Equations

    Last week, Hamed learned about a new type of equations in his math class called Modular Equations. L ...

  9. TCP Wrappers原理及简单实验

    1.TCP Wrappers(简易防火墙)简介TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接(TCP)的特定服务进行安全检测并实现访问控制,界定方式是凡是调用libwr ...

  10. scu-4445

    Right turn frog is trapped in a maze. The maze is infinitely large and divided into grids. It also c ...