duplicate key value violates unique constraint "django_admin_log_pkey"

2,983 views
Skip to first unread message

glob...@iamsandiego.com

unread,
Oct 14, 2010, 11:24:30 AM10/14/10
to Django users
Hi Folks,

After updating a postgres db for my django app Iam getting this error
when I try and save new data to the db:

Request Method: POST
Request URL: http://10.50.253.200/admin/survey/gps/add/
Django Version: 1.2 beta 1
Exception Type: IntegrityError
Exception Value:

duplicate key value violates unique constraint "django_admin_log_pkey"

Is there any way to correct this?

Thanks

Jonathan Barratt

unread,
Oct 14, 2010, 12:23:37 PM10/14/10
to django...@googlegroups.com

Sounds to me like your DB update has reset the sequence "django_admin_log_id_seq" used by postgres to get the next value for the "auto-increment"-type id field used by django_admin_log.

I would manually reset the sequence via the following:

First get the id value of the last admin entry:

select id from "django_admin_log" order by id desc limit 1;

Then set the next value of the ID sequence to that + 1 via:

select setval('django_admin_log_id_seq', LASTID+1);

(replacing LASTID+1 with the result of the first query +1 obviously...)

The other option would be to "delete * from 'django_admin_log';" so that the old ids are gone and you can start over from 1 - but then you lose your whole admin history which seems sub-optimal.

Haven't run into the problem myself though, so this is just a guess, and from a Django newbie so...

Hope it helps!
Jonathan

glob...@iamsandiego.com

unread,
Oct 14, 2010, 2:09:54 PM10/14/10
to Django users
Jonathan -

Thank you - select setval('django_admin_log_id_seq', LASTID+1); -
worked !

I can breathe again.

On Oct 14, 9:23 am, Jonathan Barratt <jonathan.barr...@knifeict.com>
wrote:
> On 14 ต.ค. 2010, at 22:24, global...@iamsandiego.com wrote:
>
> > Hi Folks,
>
> > After updating a postgres db for my django app Iam getting this error
> > when I try and save new data to the db:
>
> > Request Method:    POST
> > Request URL:      ##

Christoph Pingel

unread,
Sep 18, 2012, 3:05:20 AM9/18/12
to django...@googlegroups.com
Thanks, this helped me too 2 years later.. :-)

Tech-Harvester

unread,
Jun 1, 2017, 5:58:53 AM6/1/17
to Django users
Helped me too. Thanks.
Reply all
Reply to author
Forward
0 new messages