As far as I know Postgres uses "sequences" for generating the model IDs.
If you set the id manually I think you are bypassing the sequence,
which doesn't autoincrement. So when you use the sequence the next
time the ID clashes.
pgAdmin should auto generate the id if you leave the field blank.
On 2/16/17, Matthew Pava wrote:
> 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/5d108192354c470ca56946d28ffe02b7%40ISS1.ISS.LOCAL.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CALn3ei3ZjhY%2BYfZyJUzZamA8t1t3JUw37_mGm%2BY4ymFG%2Bq%3D7Cw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.