title: MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails
body: <p>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:</p>
  
  <pre><code>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`))
  </code></pre>
  
  <p>The database is MySQL, and I am using Workbench to inspect it; I also know that all the database engines are InnoDB and the Row Formats are Compact. I have three tables: tweets, hashtags, and references.
  Here is where I create the tables:</p>
  
  <pre><code>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 + ")" 
                  + ")");
  </code></pre>
  
  <p>Here are the statements I use to insert and the methods to insert them (I am using prepared statements):</p>
  
  <pre><code>/* 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;
  </code></pre>
  
  <p>public void insertHashtag(FluTweet tweet, int hashIndex, Integer tweetID) {</p>
  
  <pre><code>    // 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();
      }
  
  }
  </code></pre>
  
  <p>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.</p>