zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    (转)Linux系统中sysctl命令详解 sysctl -p、sysctl -a、sysctl -w
    EM2 MP1 Vowel and Consonant Teacher:Ashley
    Phonics 自然拼读法 y,x,ch,sh,(voiced)th/ð/ ,(unvoiced) th/θ/ Teacher:Lamb
    java列表转成 int[] 的格式
    分类模型评估之ROC-AUC曲线和PRC曲线
    hive 抽样方法
    AUC理解
    Spark性能调优——基础篇
    scala 稀疏向量
    scala 建模
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13314254.html
Copyright © 2011-2022 走看看