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


    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;

  • 相关阅读:
    范仁义css3课程---5、css的继承、层叠和特殊性
    范仁义css3课程---4、css常用选择器
    心得体悟帖---200103(路是我自己选的)
    心得体悟帖---200103(变化的观点)(我是对的)
    心得体悟帖---200103(开心与否更看内心)(不要丢失希望)
    心得体悟帖---200103(看似感伤)(不计较)
    windows的80端口被system进程占用的一个可能原因
    windows如何关闭mysql服务
    范仁义css3课程---3、css最常用选择器
    ImageView类简介
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13314254.html
Copyright © 2020-2023  润新知