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 ...
随机推荐
- Geoserver+Openlayers+MySQL设计思想,GeoServer服务器搭建(Docker构建镜像)
Geoserver+Openlayers+MySQL设计思想,GeoServer服务器搭建(Docker构建镜像) 一.geoserver+openlayers+mysql主要设计思想 1.1 Geo ...
- 前台console调试技巧
前台console调试技巧 一.console.log() 二.console.warn() 三.console.dir() 四.console.table() 五.console.assert() ...
- Go语言学习笔记(1)——顺序编程
Go语言学习笔记这一堆主要是<Go语言编程>(人民邮电出版社)的读书笔记.中间会穿插一些零碎的点,比如源码学习之类的.大概就是这样吧. 1. 顺序编程 1.1 变量 变量的声明: var ...
- 使用ganglia 实现监控 hadoop 和 hbase(详细过程总结)
一,环境准备 hadoop 2.8.2 分布式环境(三个节点 安装请参考 hadoop分布式环境安装) hbase 1.2.6 分布式环境(三个节点 ,安装参考hbase分布式环境安装 ) 主节点采 ...
- File类以及IO流
1.File类 package LESSON12; import java.io.File; import java.io.IOException; import java.text.SimpleDa ...
- PTA刷题记录
考虑到PAT甲级考试和开学后的XCPC比赛,决定寒假把PAT (Advanced Level) Practice刷完,进度条会在这篇博客下更新.由于主要以记录为主,大体上不会像单篇题解那么详细,但是对 ...
- HDU6311 Cover【欧拉路径 | 回路】
HDU6311 Cover 题意: 给出\(N\)个点的简单无向图,不一定联通,现在要用最少的路径去覆盖所有边,并且每条边只被覆盖一次,问最少路径覆盖数和各条路径 \(N\le 10^5\) 题解: ...
- 2019牛客多校 Round4
Solved:3 Rank:331 B xor 题意:5e4个集合 每个集合最多32个数 5e4个询问 询问l到r个集合是不是都有一个子集的xor和等于x 题解:在牛客多校第一场学了线性基 然后这个题 ...
- poj 1696 极角排序(解题报告)
#include<iostream> #include<cmath> #include<algorithm> using namespace std; double ...
- 2019牛客暑期多校训练营(第四场)k题、j题
传送门 k题: 题意: 给你一串由数字构成的字符串,你从这个字符串中找子字符串使这个字符串是300的倍数 题解: 这道题和第三场的B题极其相似 首先可以把是三百的倍数分开,必须要是100和3的倍数 是 ...