简体   繁体   English

MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败

[英]MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

I have probed numerous questions and documentation on this error and cannot seem to find where I am going wrong. 我已经探究了很多有关此错误的问题和文档,但似乎找不到我要去哪里。 The error is on this line: 错误在此行上:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Cannot add or update a child row: a foreign key constraint fails (`flutweets`.`refs`,    
CONSTRAINT `refs_ibfk_1` FOREIGN KEY (`id`) REFERENCES `tweets` (`id`))

The database is MySQL, and I am using Workbench to inspect it; 该数据库是MySQL,我正在使用Workbench对其进行检查。 I also know that all the database engines are InnoDB and the Row Formats are Compact. 我也知道所有数据库引擎都是InnoDB,行格式很紧凑。 I have three tables: tweets, hashtags, and references. 我有三个表:tweet,主题标签和引用。 Here is where I create the tables: 这是我创建表的地方:

tweets Table

Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE " + TWEETS_TABLE + "("
                + TWEETS_ID + " BIGINT NOT NULL AUTO_INCREMENT,"
                + TWEETS_TIME + " VARCHAR(255),"
                + TWEETS_USER + " VARCHAR(255),"
                + TWEETS_TEXT + " VARCHAR(255) NOT NULL,"
                + TWEETS_LOCATION + " VARCHAR(255),"
                + TWEETS_CITY + " VARCHAR(255),"
                + TWEETS_PROVINCE + " VARCHAR(255),"
                + TWEETS_COUNTRY + " VARCHAR(255),"
                + TWEETS_TIME_ZONE + " VARCHAR(255),"
                + TWEETS_LATITUDE + " DECIMAL(20,16),"
                + TWEETS_LONGITUDE + " DECIMAL(20,16),"
                + "PRIMARY KEY (" + TWEETS_ID + ")"
                + ")");
       . . .

hashtags Table

Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE " + HASHTAG_TABLE + "("
                + HASHTAG_ID + " BIGINT NOT NULL AUTO_INCREMENT,"
                + HASHTAG_TEXT + " VARCHAR(255),"
                + TWEETS_ID + " BIGINT NOT NULL,"
                + "PRIMARY KEY (" + HASHTAG_ID + ")," 
                + "FOREIGN KEY (" + TWEETS_ID + ")"
                + "REFERENCES " + TWEETS_TABLE
                + "(" + TWEETS_ID + ")" 
                + ")");
      . . .

references Table

Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE " + REF_TABLE + "("
                + REF_ID + " BIGINT NOT NULL AUTO_INCREMENT,"
                + REF_TEXT + " VARCHAR(255),"
                + TWEETS_ID + " BIGINT NOT NULL,"
                + "PRIMARY KEY (" + REF_ID + ")," 
                + "FOREIGN KEY (" + TWEETS_ID + ")"
                + "REFERENCES " + TWEETS_TABLE
                + "(" + TWEETS_ID + ")" 
                + ")");

Here are the statements I use to insert and the methods to insert them (I am using prepared statements): 这是我用来插入的语句和插入它们的方法(我正在使用准备好的语句):

/* INITIALIZE PREPARED STATEMENTS */

// Tweets
private static final String TWEETS_INSERT = "INSERT INTO " + TWEETS_TABLE
        + " (" + TWEETS_TIME + ", " + TWEETS_USER + ", " + TWEETS_TEXT + ", " + TWEETS_LOCATION + ", " + TWEETS_CITY + ", " + TWEETS_PROVINCE + ", "
        + TWEETS_COUNTRY + ", " + TWEETS_TIME_ZONE + ", " + TWEETS_LATITUDE + ", " + TWEETS_LONGITUDE + ")"
        + " VALUES(?,?,?,?,?,?,?,?,?,?)";
private PreparedStatement tweetsStatement;

// Hashtags
private static final String HASHTAGS_INSERT = "INSERT INTO " + HASHTAG_TABLE
        + " (" + HASHTAG_TEXT + ", " + TWEETS_ID + ")" + " VALUES(?,?)";
private PreparedStatement hashStatement;

// References
private static final String REF_INSERT = "INSERT INTO " + REF_TABLE
        + " (" + REF_TEXT + ", " + TWEETS_ID + ")" + " VALUES(?,?)";
private PreparedStatement refStatement;

public void insertHashtag(FluTweet tweet, int hashIndex, Integer tweetID) { public void insertHashtag(FluTweet tweet,int hashIndex,Integer tweetID){

    // Pull all of the Hashtag's relevant elements.

    String hashText =  tweet.getHashtag(hashIndex);

    try {
        hashStatement.setString(1, hashText);
        hashStatement.setString(2, tweetID.toString());

        //INSERT HASHTAG INTO TABLE
        hashStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

public void insertReference(FluTweet tweet, int referenceIndex, Integer tweetID) {

    // Pull all of the Reference's relevant elements.

    String refText =  tweet.getReference(referenceIndex);

    try {
        refStatement.setString(1, refText);
        refStatement.setString(2, tweetID.toString());

        //INSERT REFERENCE INTO TABLE
        refStatement.executeUpdate();

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

I have no idea why this is not working, any help would be much appreciated. 我不知道为什么这不起作用,任何帮助将不胜感激。 Like I said, I have probed numerous other posts and cannot find relevant help. 就像我说的,我已经探究了许多其他帖子,但是找不到相关的帮助。

Background 背景

I found out the answer to my own question. 我找到了自己问题的答案。 It was never a problem with any of the actual database handling. 任何实际的数据库处理都从来没有问题。 It turns out, when I was passing the id to be used as the foreign id in hashtags and references, I was getting my id from a counter in the main class; 事实证明,当我将ID用作标签和引用中的外来ID时,我是从主类的计数器获取ID的。 where every new tweet had a number that went with each hashtag and reference for that tweet that would later be used as a foreign key. 每个新的推文都有一个数字,该数字与每个#标签和该推文的引用一起用作后来的外键。 That foreign key would reference the primary key in the tweets table, which auto increments. 该外键将引用tweets表中的主键,该表会自动递增。

Here was the problem: 问题出在这里:

It turns out that MySQL is NOT zero-indexed when you auto-increment; 事实证明,当您自动增加MySQL时,索引不会为零。 where the first value is zero, then one, then two, etc. Instead, it starts at one. 其中第一个值是零,然后是一个,然后是两个,依此类推。相反,它从1开始。 So when I tried to reference the primary key with the foreign key, the counter that started at zero for every tweet that I assigned (the foreign key) was being incorrectly matched to the auto incrementing tweet id in the tweets table (primary key). 因此,当我尝试使用外键引用主键时,针对我分配的每个推文(外键)从零开始的计数器与推文表(主键)中的自动递增推文ID不正确匹配。

Therefore, I was always off by one . 因此,我总是一个人离开。

I hope this helps anyone that may have a similar issue. 我希望这对可能遇到类似问题的任何人有所帮助。

如果未设置有效的子类对象主键值,则可以看到MySQLIntegrityConstraintViolationException。如果忽略子类对象的主键值,则基于hibernate pojo / hbm.xml文件一级缓存中设置的配置(会话)将其刷新到数据库中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败 - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails .jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束在注册和注册期间失败 - .jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails during signup and registration Hibernate:无法添加或更新子行:外键约束失败 - Hibernate :Cannot add or update a child row: a foreign key constraint fails Hibernate 无法添加或更新子行:外键约束失败 - Hibernate 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 Hibernate 无法添加或更新子行:外键约束在休眠状态下失败 - Cannot add or update a child row: a foreign key constraint fails in hibernate 休眠:无法添加或更新子行:外键约束失败 - Hibernate:Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败 - 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 - Hibernate 外键约束无法添加或更新数据库“无法添加或更新子行:外键约束失败” - foreign key constraint can not add or update database“ Cannot add or update a child row: a foreign key constraint fails ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM