Skip to main content
August 20, 2021
Question

#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`))

  • August 20, 2021
  • 6 replies
  • 0 views

#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?

    6 replies

    Participating Frequently
    August 20, 2021

    Seems like a FK constraint issue.

    In cloud DB select your table and see the FK constraints and verify you are sending the correct data to your fk

    August 20, 2021

    This is the error I got when creating the relationship between two tables using PK and FK combination. 

    peter.lewis
    Employee
    August 20, 2021

    How did you try to create that relationship? Are you seeing the error in Appian or in the database?

    selvakumark
    Participating Frequently
    August 22, 2021

    [mention:c0e54ea7821d4462a82e8404d41120d1:e9ed411860ed4f2ba0265705b8793d05]

    Since you are getting this error while creating the relationship itself, I think your foreign key column contains values that are not present in the parent table as a primary key.

    For example, let's say tableA is the parent table & the primary key column is colA. It contains 10 rows of data hence the primary key values are 1,2,3,4,5,6,7,8,9,10. Your child table is tableB & the foreign key column is colB. If the colB has values other than the present values such as 0 (or) greater than 10, then the database will not allow adding the constraint because it fails.

    Try deleting such values from your child table & then perform this operation.

    August 22, 2021

    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.

    ravir0002
    August 23, 2021

    If you are establishing the FK relation while creating the CDT, please make sure you provide the fk column name all in small(the column name to be mapped).