This is a fascinating issue, fellow Django users.
I have a model called Lookups that is basically just an auto-generated pk and a name. I added some data to that table using pgAdmin 3 since I’m using a PostgreSQL backend.
One of my users then filled out a form that ran some code that would ‘get_or_create’ on the Lookups model. It threw an IntegrityError saying that the Key (id)=# already exists.
Upon further investigation, I realized that the id # was one that I had already entered manually in the database several days ago. Only after the user kept running that form until the id incremented to one that did not exist did the IntegrityError go away.
Maybe I entered the id manually in pgAdmin 3 instead of letting it do the auto-generation and that’s why it broke?
But Django was generating the proper SQL, so I can’t imagine how Django could possibly be involved in addressing this issue:
'INSERT INTO "general_lookups" ("name") VALUES (%s) RETURNING "general_lookups"."id"'
Maybe it’s more a problem with PostgreSQL, but I wanted to share this with you in case you ever run into this issue. I think for now, I should just use Django to enter data into the database, or test pgAdmin to auto-increment the id with manual data entry.