MySQL - Cannot add or update a child row: a foreign key constraint fails

后端 未结 9 637
执念已碎
执念已碎 2021-01-07 19:25

This seems to be a common error, but for the life of me I can\'t figure this out.

I have a set of InnoDB user tables in MySQL that are tied together via foreign key;

相关标签:
9条回答
  • 2021-01-07 20:16

    Since you haven't given table definitions, it's hard to guess. But it looks like you are attempting to modify the foreign key in the child table. AFAIK, this is illegal, you can modify it from the parent, but not the child table.

    Consider this example:

    CREATE TABLE parent (
      parent_id INT NOT NULL,
      parent_data int,
    
      PRIMARY KEY (parent_id)
    ) ENGINE=INNODB;
    
    CREATE TABLE child1 (
      child1_id INT,
      child1_data INT,
      fk_parent_id INT,
    
      INDEX par_ind1 (fk_parent_id),
    
      FOREIGN KEY (fk_parent_id)
        REFERENCES parent(parent_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
    ) ENGINE=INNODB;
    
    CREATE TABLE child2 (
      child2_id INT,
      child2_data INT,
      fk_parent_id INT,
    
      INDEX par_ind2 (fk_parent_id),
    
      FOREIGN KEY (fk_parent_id)
        REFERENCES parent(parent_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
    ) ENGINE=INNODB;
    
    INSERT INTO parent
      (parent_id, parent_data)
      VALUES
      (1, 11),
      (2, 12);
    
    INSERT INTO child1
      (child1_id, child1_data, fk_parent_id)
      VALUES
      (101, 1001, 1),
      (102, 1002, 1),
      (103, 1003, 1),
      (104, 1004, 2),
      (105, 1005, 2);
    
    INSERT INTO child2
      (child2_id, child2_data, fk_parent_id)
      VALUES
      (106, 1006, 1),
      (107, 1007, 1),
      (108, 1008, 1),
      (109, 1009, 2),
      (110, 1010, 2);
    

    Then this is allowed:

    UPDATE parent
      SET parent_id = 3 WHERE parent_id = 2;
    
    SELECT * FROM parent;
    SELECT * FROM child1;
    SELECT * FROM child2;
    

    But this is not, because it modifies the parent fk from the child table:

    UPDATE child1
      SET fk_parent_id = 4 WHERE fk_parent_id = 1;
    

    It gets an error very similar to your error:

    Cannot add or update a child row: a foreign key constraint fails (`db_2_b43a7`.`child1`, CONSTRAINT `child1_ibfk_1` FOREIGN KEY (`fk_parent_id`) REFERENCES `parent` (`parent_id`) ON DELETE CASCADE ON UPDATE CASCADE):
    
    0 讨论(0)
  • 2021-01-07 20:17

    I had faced same issue while creating foreign constraints on table. the simple way of coming out of this issue are first take backup of your parent and child table then truncate child table and again try to make a relation. hope this will solve the problem.

    0 讨论(0)
  • 2021-01-07 20:17

    Even though this is pretty old, just chiming in to say that what is useful in @Sidupac's answer is the FOREIGN_KEY_CHECKS=0.

    This answer is not an option when you are using something that manages the database schema for you (JPA in my case) but the problem may be that there are "orphaned" entries in your table (referencing a foreign key that might not exist).

    This can often happen when you convert a MySQL table from MyISAM to InnoDB since referential integrity isn't really a thing with the former.

    0 讨论(0)
提交回复
热议问题