115
Views
3
Comments
Solved
The INSERT statement conflicted with the FOREIGN KEY constraint - Please Clarify
Question

So there are a half-dozen forum posts with this title.  I've read through all of them, but I still don't understand.

Most of the responses are something like this:

This error means that you are trying to create a record in the entity, but looks like the entity has a mandatory Foreign Key that is not being provided, or the value provided does not match one in that entity.  

Here's my confusion.  I am using a CreateOrUpdate record step.  I thought that if I passed a record to this step (with no ID), that the step would automatically create an ID for the record.  Isn't that one of the fundamental things that any database does?  

I have a record in memory called Offering.  I send that record to the CreateOrUdateOffering step and it should save that record in the database with a new ID.  To be clear, I'm making a new record using the UI.  Nowhere in the UI do I ask the user to provide an ID (nor should anyone).  So, there is no ID being sent to the database.

In fact the output of the CreateOrUpdate step is ID.  If I already knew what the ID was before I sent it to the step that would redundant.  The fact that ID is output indicates to me that the step generates the ID and tells me what it is when it's done creating the record.

Can anyone help me out?  Does Outsystems not automatically create unique IDs for newly created records?  

2021-04-17 09-53-48
José Gonçalves
Solution

Hi,

the foreign key here is not the Id of the table you're inserting a record, but another Id attribute for another table.

i.e. the table (Client) below have a foreign key CountryId. If that attribute is mandatory, in the insert process you need to pass a valid Id for that field.

Client

Id

Name

CountryId


Regards

2016-04-22 00-29-45
Nuno Reis
 
MVP

Hello Terry.

To be exact, OutSystems (like SQL) only creates ids if you set the id to be a Autonumber. All the other cases, you have to provide a value.

Your issue, like José mentioned, is because you have some external id in the attributes of Offering that has in invalid value or does not match the other entity.

UserImage.jpg
Terry Weatherstone

Holy Foreign Keys, Batman -  I see what you're saying.  The record I'm trying to create has no problem with ITS foreign keys.  But there is a link to another table in my data structure that isn't provided.  

Thanks gentlemen!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.