#1452 - Cannot add or update a child row: a foreign key constraint fails (`Appian`.`#sql-alter-34ce-15086d9`, CONSTRAINT `tems_stability_ranges_ibfk_2` FOREIGN KEY (`budget_id`) REFERENCES `tems_etem_log_excursion` (`budget_id`))

#1452 - Cannot add or update a child row: a foreign key constraint fails (`Appian`.`#sql-alter-34ce-15086d9`, CONSTRAINT `table1_ibfk_2` FOREIGN KEY (`first_id`) REFERENCES `table2` (`first_id`))

What does this error mean?

  Discussion posts and replies are publicly visible

Parents
  • Hi Shubha, 

    You can find the rows present in the child table that is causing the conflict while establishing the foreign key constraint by using the below query:

    (Please change the table name and column name according to your scenario)

    SELECT * FROM childTable WHERE childTablePrimaryKey IN
    (SELECT childTablePrimaryKey from childTable WHERE NOT EXISTS
    (Select 1 from parentTable where parentTable.foreignkeyColumn = childTable.foreignkeyColumn)
    ) AND foreignkeyColumn IS NOT NULL;

    The result of above query will give you the conflicting rows and please try deleting those rows and then execute the foreign key creation query.

Reply
  • Hi Shubha, 

    You can find the rows present in the child table that is causing the conflict while establishing the foreign key constraint by using the below query:

    (Please change the table name and column name according to your scenario)

    SELECT * FROM childTable WHERE childTablePrimaryKey IN
    (SELECT childTablePrimaryKey from childTable WHERE NOT EXISTS
    (Select 1 from parentTable where parentTable.foreignkeyColumn = childTable.foreignkeyColumn)
    ) AND foreignkeyColumn IS NOT NULL;

    The result of above query will give you the conflicting rows and please try deleting those rows and then execute the foreign key creation query.

Children
No Data