Cannot add or update a child row a foreign key constraint f

Posted odejsjhshw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cannot add or update a child row a foreign key constraint f相关的知识,希望对你有一定的参考价值。

1、错误描述

 

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails 
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
	at com.mysql.jdbc.Util.getInstance(Util.java:387)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
	at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
	at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
	at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
	at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100)
	at org.logicalcobwebs.proxool.ProxyStatement.intercept(ProxyStatement.java:57)
	at $java.lang.AutoCloseable$$EnhancerByProxool$$9c7ad94b.executeUpdate(<generated>)
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
	... 61 more

2、错误原因

    外键约束失败,导致不能添加或修改数据

 

3、解决办法

(1)通过设置FOREIGN_KEY_CHECKS 变量

         添加或修改前设置:

         SET FOREIGN_KEY_CHECKS=0;

          添加或修改后设置:

          SET FOREIGN_KEY_CHECKS=1;

 

(2)暂时关闭外键唯一性检测

 

         添加或修改前设置:

         SET UNIQUE_CHECKS=0;

         添加或修改后设置:

         SET UNIQUE_CHECKS=1;

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow


以上是关于Cannot add or update a child row a foreign key constraint f的主要内容,如果未能解决你的问题,请参考以下文章

Django报错修复:1452, ‘Cannot add or update a child row: a foreign key constraint fails

hibernate错误:Cannot add or update a child row: a foreign key constraint fails

insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.`products

ERROR 1452 : Cannot add or update a child row: a foreign key constraint fails

Django报错修复:1452, ‘Cannot add or update a child row: a foreign key constraint fails

Django报错修复:1452, ‘Cannot add or update a child row: a foreign key constraint fails