Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm using Django, and every once in a while I get this error:

IntegrityError: duplicate key value violates unique constraint "myapp_mymodel_pkey"
DETAIL:  Key (id)=(1) already exists.

My database does in fact have a myapp_mymodel object with the primary key of 1.

Why would Postgres attempt to use that primary key again? Or, is this most likely my application (or Django's ORM) causing this?

Note: I'm using Postgres.

share|improve this question
what makes you certain that it's postgres? i'd be more inclined to believe that it's the ORM (or something in the code that's being passed to the ORM). –  swasheck Jul 11 '13 at 4:06
@swasheck - I'm not certain about anything, to be honest. The fact that this error is so intermittent (happened only 3 or so times in 2 weeks - no other load on the DB, just me testing out my application) is what makes me so wary of a low-level problem. –  orokusaki Jul 11 '13 at 12:55

2 Answers 2

up vote 7 down vote accepted

I fully agree with swasheck. PostgreSQL will not try to insert duplicate values on its own, it is you (your application, ORM included) who does.

It can be either a sequence feeding the values to the PK set to the wrong position and the table already containing the value equal to its nextval() - or simply that your application does the wrong thing. The first one is easy to fix:

SELECT setval('your_sequence_name', (SELECT max(id) FROM your_table));

The second one means debugging.

share|improve this answer
+1. Smells to me like the ORM does not know it is a sequence and inserts a fixed value - over and over again. –  TomTom Jul 11 '13 at 9:15
If Django was to "reset sequences", could this cause the problem? Also, if I insert a row over top an existing row, with the primary key set to 1 (using a Django fixture), could that cause this problem to later occur? –  orokusaki Jul 11 '13 at 13:00
I would say Django (or any other popular framework) doesn't do that on its own - otherwise we would have similar questions every other day. The second part I don't fully understand, but yes, if you try to insert 1 twice, you will get exactly this error :) –  dezso Jul 11 '13 at 13:06
This issue occurred 3 more times in a row just now. What I've found is that when it does occur it happens one or more times in a row for a given table, then not again. It seems to happen for every table before it completely stops for days, happening for at least a minute or so per table when it does occur, and only happening intermittently (not all tables right away). –  orokusaki Jul 12 '13 at 3:15
@orokusaki It sounds like a fun debugging session... –  dezso Jul 12 '13 at 8:10

I ended up here with very same error, which was occuring rarely, and was hard to track, because I was loking for it not where I should.

Fault was JS repetition which was doing the POST to the server twice! So sometimes it is worth to have a look not only on your django (or any other web framework) views and forms but also what happens on very front side.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.