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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Django specifically states that the primary key is generated by the DBMS unless specified - now, I don't know what @orokusaky was doing in his python code, but I ended up on this page because I'm quite confident that I have no code trying to use a specific primary key and I've never seen a DBMS trying to use a wrong one. – mccc Oct 9 '15 at 8:32
up vote 8 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
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
@orokusaki It sounds like a fun debugging session... – dezso Jul 12 '13 at 8:10
I have the same problem happening with postgres and sqlalchemy as the ORM, and I've verified the ORM is not passing the PK explicitly. Still researching. – charlax Oct 1 '15 at 21:58
Happened to me as well, once in six months.. It seemed like a wrong sequence position, as the new pk value was a couple rows behind and kept increasing with subsequent tries. Quite weird, tbh, and I have no idea or clue how that might have happened. – mccc Oct 9 '15 at 8:24

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.