543
Views
8
Comments
Solved
CreateOrUpdate : Violation of PRIMARY KEY constraint
Question

I'm testing the CreateOrUpdate action behaviour.


The primary key is of text type and is a concatenation of other attributes


The first row is inserted with the specified text primary key.

Id: Test1Test2

Attribute1: Test1

Attribute2: Test2

Attribute3: Test3


Then, using the same primary key, a new updated record is created. 

Id: Test1Test2

Attribute1: Test1

Attribute2: Test2

Attribute3: Test3-update


When passing this new record to the CreateOrUpdate action, the error is raised - Violation of PRIMARY KEY constraint 'OSPRK_OSUSR_KC0_CREATEORUPDATE3'. Cannot insert duplicate key in object 'dbo.OSUSR_KC0_CREATEORUPDATE3'. The duplicate key value is (Test1Test2).


How to overcome this?


2019-06-15 21-39-22
Afonso Carvalho
 
MVP
Solution

This is the issue:

Your last action is not a CreateOrUpdate: it's a Create. The difference is that it won't try to update an existing record but it will force the database to create a new record. This is what causes your primary key problem.

Switch it to use the second action on that list (CreateOrUpdateCreateOrUpdate) and you shouldn't have any problems.

2019-06-15 21-39-22
Afonso Carvalho
 
MVP

Hi Nuno,

The CreateOrUpdate Entity actions will always attempt to update existing records. Are you sure you're using CreateOrUpdate instead of Create? Can you show us screenshots of your logic and Entity?

UserImage.jpg
Nuno Gomes

Afonso Carvalho wrote:

Hi Nuno,

The CreateOrUpdate Entity actions will always attempt to update existing records. Are you sure you're using CreateOrUpdate instead of Create? Can you show us screenshots of your logic and Entity?


Thank you very much Afonso. In attach the files

createOrUpdatetest.zip
2019-06-15 21-39-22
Afonso Carvalho
 
MVP

Both actions in the main flow say "CreateCreateOrUpdate". It's the "Create" part that I'm finding a bit odd. 

Can you validate that the Action is indeed a CreateOrUpdate? If it starts with just Create, then it will be the cause of your error.

Would you feel comfortable sharing the application?

UserImage.jpg
Nuno Gomes

Afonso Carvalho wrote:

Both actions in the main flow say "CreateCreateOrUpdate". It's the "Create" part that I'm finding a bit odd. 

Can you validate that the Action is indeed a CreateOrUpdate? If it starts with just Create, then it will be the cause of your error.

Would you feel comfortable sharing the application?

Thanks. Sure, here it goes


CreateOrUpdateTest.oap
2019-06-15 21-39-22
Afonso Carvalho
 
MVP
Solution

This is the issue:

Your last action is not a CreateOrUpdate: it's a Create. The difference is that it won't try to update an existing record but it will force the database to create a new record. This is what causes your primary key problem.

Switch it to use the second action on that list (CreateOrUpdateCreateOrUpdate) and you shouldn't have any problems.

UserImage.jpg
Nuno Gomes

Thanks Afonso! Next time I'll choose other names for the actions ':)

2019-06-15 21-39-22
Afonso Carvalho
 
MVP

Yeah, that Entity name is cursed. Lesson learned!

UserImage.jpg
anshuman takle

Hello i want one help my application not work when i send message so they generate same error of voilation of primary key, user name james and password is 123, i try to make chat application please help me out from this error.

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