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;

  • 相关阅读:
    解决tmux在PuTTY下工作异常的问题
    使用 Tmux 强化终端功能
    Redis的五种数据结构
    Kubernetes(k8s) docker集群搭建
    C# 正则表达式大全
    C#异步编程(async and await)及异步方法同步调用
    ASP.NET MVC同时支持web与webapi模式
    ActiveX IE保护模式下的低权限操作路径及Windows操作系统特殊路径
    C#文件夹权限操作工具类
    C#创建文件夹并设置权限
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13314254.html
Copyright © 2011-2022 走看看