#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
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
This is the error I got when creating the relationship between two tables using PK and FK combination.
How did you try to create that relationship? Are you seeing the error in Appian or in the database?
shubhamy0001
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.
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.
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).