8

I am trying to run the following query:

update OAR.oa_AcademicHead
set  personid=15857
where personid=1234

but I am getting the error:

Cannot insert duplicate key row in object 'OAR.oa_AcademicHead' with unique index 'IX_oa_AcademicHead_personid'.
The statement has been terminated.

What can be done to fix this??

2
  • 3
    why in the world would you want to change the primary id of a row. isn't it like, against any database integrity principle? Commented Mar 5, 2014 at 21:35
  • Maybe you just want to delete 1234? I'm rather fascinated by this idea of people merging into each other. Commented Mar 5, 2014 at 22:32

4 Answers 4

18

A row where personid is 15857 already exists in the table. The unique index on that column is preventing you from committing another record with the same personid (an update is really a delete and insert).

You'll have to remove the existing person with that id first before running your query.*

* Disclaimer: make sure this is actually what you want to do; be aware of the issues it might cause.

Sign up to request clarification or add additional context in comments.

2 Comments

There is only one row with the personid 15857 in the given table and personid is foreign to key to some other table and primary key of the table is academicheadid. Why can't we have two rows with the same person id. Also, I cannot delete the person id 1234 because he is academic of some other organization also. So, if I delete 1234 the information regarding is AH status will be lost. What else can be done to fix it? Thanks.
The index IX_oa_AcademicHead_personid is specified as being a unique index, meaning every personid in the table must be unique; there can be no duplicates. That "some other table" from which it is a foreign key sounds like a "Person" table. In reality, two different academic heads cannot be the same person, hence the unique index. Without seeing the entire database schema it would be hard to suggest a course of action.
4

I am aware that this question is really old. But still might be helpful.

I've experienced this problem with my database and managed to fix it after a few hours of struggling.

My database has an Attachments table, which has primary key ID. I also have messageID column and was not able to create two rows with same messageID. I have found that messageID is indexer. I don't know what are indexers used for exactly, but I simply deleted indexer and now it works fine.

picture of my database and where I found indexer

2 Comments

I know this comment is really old on a question that is even older, but I need to say - THIS IS TERRIBLE ADVICE! The index is there for a reason, and that reason is to enforce uniqueness. When you remove the index you remove the requirement for uniqueness. This is like throwing away your fire alarm because it keeps waking you up when there's fires. The index was literally doing the exact job it was put in place for, and now you probably have a table full of nonsense duplicates.
Agreed. Deleting an index without having any idea what they are is a terrible idea.
0

If you get said error while using ASP.NET MVC do something like this:

db.Entry(personObject).State = EntityState.Modified;
db.Entry(personObject).Property(x => x.personid).IsModified = false;
db.SaveChanges();

Comments

0

If your model is decorated with an indexer like [Index(nameof(SomeForeignKey), nameof(SomeOtherForeignKey), IsUnique = true)], then you will counter problems like those, like "An error occurred while updating the entries. See the inner exception for details."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.