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

 lmaster 发布于 2023-01-12 12:40

我已经探讨了很多关于这个错误的问题和文档,似乎无法找到我出错的地方.错误在这一行:

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`))

数据库是MySQL,我使用Workbench来检查它; 我也知道所有数据库引擎都是InnoDB,而Row Formats是Compact.我有三个表:推文,主题标签和引用.这是我创建表的地方:

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 + ")" 
                + ")");

以下是我用来插入的语句和插入它们的方法(我使用的是预处理语句):

/* 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){

    // 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();
    }

}

我不知道为什么这不起作用,任何帮助将不胜感激.就像我说的,我已经探测了很多其他帖子,但找不到相关的帮助.

1 个回答
  • 背景

    我找到了自己问题的答案.任何实际的数据库处理都不是问题.事实证明,当我传递id用作标签和引用中的外来id时,我从主类中的计数器获取我的id; 每个新推文都有一个数字与每个主题标签和该推文的引用一起使用,该推文稍后将用作外键.该外键将引用tweets表中的主键,该表自动递增.

    这是问题所在:

    事实证明,当你自动增加时,MySQL 不是零索引的; 第一个值为零,然后是一个,然后是两个,等等.相反,它从一个开始.因此,当我尝试使用外键引用主键时,对于我指定的每条推文(外键),从零开始的计数器与推文表(主键)中的自动递增推文ID不正确匹配.

    因此,我总是一个接一个.

    我希望这可以帮助任何可能有类似问题的人.

    2023-01-12 12:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有