42
Views
7
Comments
About SQL
Question

Hi all, 

I have a question.

When CreateOrUpdate is executed, is it possible to determine whether an Insert statement or an Update statement is executed?
In either case, the following error is displayed;

Cannot insert duplicate key row in object 'dbo.***' ~

Hope to hear from you.

Regards,
S

UserImage.jpg
Supriya Malla

Hi @Sammy A,

If your entity has a unique index on any field, trying to insert a record with duplicate values for that field will cause this error.

This happens because CreateOrUpdate will try to insert a new record, but if it finds a duplicate value in the indexed field, the database won’t allow it.

Check if your entity has a unique index and make sure you're not inserting or updating with duplicate values in those fields.

For more information about createOrUpdate check out this post   https://www.outsystems.com/forums/discussion/85614/real-time-examples-to-use-create-entity-action-over-createorupdate/

Hope that helps! 

Thanks,

Supriya

2021-10-08 05-01-12
Deepa Tiwari

Hi @Sammy A ,

You need to check unique key index in entity and in server Action you can get record by the aggregate with those value is exist or not.

If it is exist then Use Update action else create action. or Base on the condition you can show message to user like record already exist with those values.


Hope this will help you.

Thanks

Deepa

2024-01-04 09-21-21
Venkatesaiya

Hi @Sammy A ,

This error occurs when you try to insert a duplicate key (value) that is already present in the table. If you have a unique index on any column in the table, you can only have one entry with that value; duplicate values are not allowed 

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

@Deepa Tiwari, @Venkatesaiya,

You are both giving the exact same answer as Supriya did, only half a day later. Please check other answers before answering a question to prevent duplicates. Thanks.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Sammy,

Ideally, you wouldn't use a CreateOrUpdate action, as that's slower than a Create or an Update action. In your entity wrapper action, check whether the Id is NullIdentifier(), and if so execute a Create, otherwise an Update.


2024-12-31 05-58-29
Atal Sharma

@Kilian Hekhuis 
I was reading all the comment as well as older forums. I don't think so its copied comment.
Everyone has their understanding on coding and providing suggestion.

In your comment I can say its copied of Deepa's Comments.

2025-08-07 06-30-56
Amit J
Champion

Common Cause of This Error:

Cannot insert duplicate key row in object 'dbo.***......

This usually means:

  • You're passing an Id ≠ 0, but that Id doesn't exist in the database.

  • OutSystems assumes Update, but the record isn't there, so it tries an Insert with the same unique key value, causing a violation.

How to Handle This Cleanly:

Option 1: Manually Check Before CreateOrUpdate

Use an Aggregate or GetBy<...> logic to check if the record exists, and then:

plaintextCopyEditIf Exists → Use UpdateElse       → Use Create

Option 2: Try/Catch Exception Handling

Wrap CreateOrUpdate in an Exception Handler, and if it fails, fall back to alternate logic. Not recommended unless necessary.

Option 3: Ensure Id Is Always Accurate

Ensure that:

  • You don’t pass a non-zero Id unless the record definitely exists.

  • You generate or fetch the correct Id using Get... before updating.


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