The INSERT statement conflicted with the FOREIGN KEY constraint when saving a record with a dropdown-type field

I’ve come across this error message when trying to save value in lookup field. It turned out that the lookup field was modelled incorrectly and it had assigned wrong lookup. Thus storing values from different lookup broke the foreign key constrain.

This is a seldom but important mistake: Let’s say you have a field refBusinessPartnerId but you mistakingly set a Lookup on your dropdown widget in the screen designer to Invoice. The dropdown will list invoices and some invoice ID will be stored in your refBusinessPartnerId field.

After you try to save the form you will get the INSERT statement conflicted with the FOREIGN KEY constraint error because you were trying to save an Invoice ID in the field which has a foreign key reference to BusinessPartner table.