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 ...
随机推荐
- 为什么要选择学习Java?适合零基础的初学者的文章
我经常收到这样的问题:"要学习的第一门编程语言是什么?" Java是一门好的编程语言吗?"和" Java是适合初学者的好的第一门编程语言,还是我应该从Java或 ...
- 五种C语言非数值计算的常用经典排序算法
摘要:排序是计算机的一种操作方法,其目的是将一组"无序"的记录序列调整为"有序"的记录序列,主要分为内部排序和外部排序. 排序 排序是计算机的一种操作方法,其目 ...
- java 静态资源,非静态资源,父类子类,构造方法之间的初始化循序
java面试经常被问静态资源,非静态资源,父类子类,构造方法之间的执行顺序.下面添加两个类做个测试 class Parent { // 静态变量 public static String p_Stat ...
- Go语言学习笔记(4)——并发编程
Golang在语言级别支持了协程,由runtime进行管理. 在Golang中并发执行某个函数非常简单: func Add(x, y int) { fmt.Println(x + y) } func ...
- Spring boot 异步线程池
package com.loan.msg.config; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandle ...
- PHP-mysql存储照片的两种方式
PHP-mysql存储照片的两种方式 方式一:把图片数据存储在数据库中(二进制) 数据库代码: CREATE TABLE `photo` ( `id` int(10) unsigned NOT ...
- DEDECMS:更改dede提示框标题
在include文件夹下,修改common.func.php文件里的 <title>DedeCMS提示信息</title> 修改成自己想要的标题. 将 <b>Ded ...
- c#的dllimport使用方法详解(Port API)
DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL(托管/非托管是微软的.net framework中特有的概念,其中, ...
- Java工作中的并发问题处理方法总结
Java工作中常见的并发问题处理方法总结 好像挺久没有写博客了,趁着这段时间比较闲,特来总结一下在业务系统开发过程中遇到的并发问题及解决办法,希望能帮到大家 问题复现 1. "设备Aの奇怪分 ...
- A - The Suspects (sars传染)
题意:有m组,0为起点,有0的那一组全是嫌疑人,之后会不断传递到其它组去,问一共有多少人是嫌疑人. Severe acute respiratory syndrome (SARS), an atypi ...