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

后端 未结 9 699
执念已碎
执念已碎 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条回答
  •  旧时难觅i
    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):
    

提交回复
热议问题