简体   繁体   English

不必要的外键约束失败

[英]Unnecessary Foreign Key Constraint Failure

So I have a mock database for a conference where I'm creating tables for the authors, papers, reviewers, etc. 因此,我有一个会议的模拟数据库,其中正在为作者,论文,审稿人等创建表格。

The reviewers provide an email which refers to the Program Committee emails. 审稿人提供了一封电子邮件,该电子邮件是指计划委员会的电子邮件。 This is the key constraint I put in place. 这是我施加的关键约束。 Then I add data to the PC table and then attempt to add data to the reviewer table. 然后,我将数据添加到PC表中,然后尝试将数据添加到审阅者表中。 This is the error I get: 这是我得到的错误:

Exception encountered com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`sampledb`.`review`, CONSTRAINT `review_ibfk_2` FOREIGN KEY (`email`) REFERENCES `pcmember` (`email`))

Here are the other functions: 以下是其他功能:

public int loadPCMember(){
    String tablename = "pcmember";
    String create = "CREATE TABLE IF NOT EXISTS pcmember(email VARCHAR(100), name VARCHAR(50), PRIMARY KEY (email));";
    makeTable(create);
    System.out.println("made table pcmember");
    //CSV Reader
    String[][] content = CSVReader(tablename,2);

    for(int i = 0 ; i < content.length; i++){
        try{
            String query = "INSERT INTO pcmember(email,name) VALUES (?,?)";
            PreparedStatement ps2 = net.prepareStatement(query);
            ps2.setString(1, content[i][0]);
            ps2.setString(2, content[i][1]);
            ps2.executeUpdate();
            System.out.println((i+1)+ " done");
        // Throw exception
        }catch (SQLException e){System.out.println("Exception encountered");return 0;}
    }
    System.out.println("PC Member Done");
    return 1;
}
//Load next
public int loadReview(){
    String tablename = "review";
    String create = "CREATE TABLE IF NOT EXISTS review(reportid INTEGER, sdate DATE, comment VARCHAR(250), recommendation VARCHAR(6), paperid INTEGER NOT NULL, email VARCHAR(100) NOT NULL, PRIMARY KEY(reportid), FOREIGN KEY (paperid) REFERENCES paper(paperid), FOREIGN KEY(email) REFERENCES pcmember(email));";
    makeTable(create);
    System.out.println("made table review");
    //CSV Reader
    String[][] content = CSVReader(tablename,6);

    for(int i = 0 ; i < content.length; i++){
        System.out.println("" + content[i][0] + "\t" +content[i][1] + "\t" + content[i][2] + "\t" +content[i][3] + "\t" +content[i][4] + "\t" +content[i][5]);
        try{
            //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
            //java.util.Date date = sdf.parse(content[i][1]);
            //System.out.println(""+date);
            //Date newDate = new Date(date.getTime());
            //System.out.println(""+newDate);
            String query = "INSERT INTO review(reportid,sdate,comment,recommendation,paperid,email) VALUES (?,?,?,?,?,?)";
            PreparedStatement ps2 = net.prepareStatement(query);
            ps2.setInt(1, Integer.parseInt(content[i][0]));
            ps2.setDate(2, java.sql.Date.valueOf(content[i][1]));
            ps2.setString(3, content[i][2]);
            ps2.setString(4, content[i][3]);
            ps2.setInt(5, Integer.parseInt(content[i][4]));
            ps2.setString(6, content[i][5]);
            ps2.executeUpdate();
            System.out.println((i+1)+ " done");
        // Throw exception
        }catch (SQLException e){System.out.println("Exception encountered "+ e);return 0;
        }//catch (ParseException e){System.out.println("Parse Exception encountered "+e);}

    }
    System.out.println("Review Done");
    return 1;
}

I have a decent understanding of the key constraints and I'm pretty spot on with the CSV files having the same exact emails, so what could be causing this error? 我对关键约束有很好的理解,而且对于具有相同确切电子邮件的CSV文件,我很满意,那么可能是什么导致此错误?

I'm a SQL guy, not a Java guy. 我是SQL专家,不是Java专家。 If I ask or suggest anything that doesn't make sense because of that - you know why. 如果我提出任何建议或建议,因为这些没有任何意义-你知道为什么。 To be clear - based on that error message and the sql you have included it looks like you trying to insert a record into the review table that has an email address that doesn't exist in the pcmember table thus violating the foreign key constraint on the review table. 需要明确的是-根据该错误消息和您所包含的sql,您似乎试图将一条记录插入审阅表中,而该记录中的电子邮件地址在pcmember表中不存在,从而违反了对查看表格。 This seems like a data problem with the CVSs you are using. 这似乎是您使用的CVS的数据问题。 However since you mentioned that you are confident in the data files is it possible that the it's trying to INSERT the rows into the review table before the pcmember INSERT has successfully completed? 但是,由于您提到您对数据文件很有信心,是否有可能在pcmember INSERT成功完成之前尝试将行插入检查表中?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM