Postgresql duplicate keys and django problem

3 views
Skip to first unread message

rikl...@gmail.com

unread,
Oct 17, 2006, 6:11:53 AM10/17/06
to Django users
Few days ago I've started to get such errors in the Admin Panel when I
want to add something:
##################################
IntegrityError: ERROR: duplicate key violates unique constraint
"rk_news3_pkey"

INSERT INTO "rk_news3"
("news_title","news_text","news_more","news_date","news_keywords")
VALUES ('testtitle','test text','','2006-10-17 12:10:00.106753','foo
bar')
###################################
It worked before and I can add things from the "user view" (forum
works). I use postgresql and django-SVN. I've reset the counters (like
ALTER SEQUENCE rk_page1_id_seq RESTART 1;) but it still throw this
error.

Michael Radziej

unread,
Oct 17, 2006, 8:52:54 AM10/17/06
to django...@googlegroups.com
rikl...@gmail.com schrieb:

We'd need to see your model. And, why did you restart the sequences?

Michael

James Bennett

unread,
Oct 17, 2006, 8:55:53 AM10/17/06
to django...@googlegroups.com
On 10/17/06, rikl...@gmail.com <rikl...@gmail.com> wrote:
> Few days ago I've started to get such errors in the Admin Panel when I
> want to add something:

I'm curious about why you reset the sequence, rather than using
setval() to push it up to a working value, but this just sounds like a
sequence that's gotten out of sync with the state of its associated
table. And without a lot more information about what's going on (how
many apps access the database, for example, and what they use to do
it), it'd be hard to track down how that happened.

--
"May the forces of evil become confused on the way to your house."
-- George Carlin

rikl...@gmail.com

unread,
Oct 18, 2006, 4:34:44 AM10/18/06
to Django users
Models look like this:

#################

class News(models.Model):
news_title = models.CharField(maxlength=255, verbose_name='Tytul')
news_text = models.TextField(verbose_name='Tresc')
news_more = models.TextField(verbose_name='Rozszerzona Tresc',
blank=True, default='')
news_date = models.DateTimeField(auto_now_add = True, blank=True)
news_keywords = models.CharField(maxlength=255, verbose_name='Slowa
Kluczowe', db_index=True)
class Meta:
verbose_name = "Nowosc"
verbose_name_plural = "Nowosci"
db_table = 'rk_news' + str(settings.SITE_ID)
class Admin:
list_display = ('news_title', 'news_date')
list_filter = ['news_date']
search_fields = ['news_title', 'news_text']
date_hierarchy = 'news_date'
fields = (
(None, {
'fields': ('news_title', 'news_text', 'news_keywords')
}),
('Rozszerzona Tresc', {
'classes': 'collapse',
'fields' : ('news_more',)
}),)
def __str__(self):
return self.news_title

class Page(models.Model):
title = models.CharField(maxlength=255, verbose_name='Tytul')
slug = models.SlugField(maxlength=255, unique=True,
verbose_name='Odnosnik', prepopulate_from=['title'])
description = models.CharField(maxlength=255, verbose_name='Opis')
text = models.TextField(verbose_name='Tresc')
creation_date = models.DateTimeField(auto_now_add = True, blank=True)
modification_date = models.DateTimeField(auto_now = True, blank=True)
class Meta:
verbose_name = "Strona"
verbose_name_plural = "Strony"
db_table = 'rk_page' + str(settings.SITE_ID)
class Admin:
list_display = ('title', 'slug')
search_fields = ['title', 'text', 'description']
fields = (
(None, {
'fields': ('title', 'slug', 'description', 'text')
}),)
def get_absolute_url(self):
return '/w/p/' + self.slug + '/'
def __str__(self):
return self.slug
######################
User views show the data and the Admin Panel is used for add/edit work.

rikl...@gmail.com

unread,
Oct 18, 2006, 6:13:29 AM10/18/06
to Django users
I've setval the counters to higher, free numbers and it looks like it
works now :)

Denis

unread,
Oct 18, 2006, 6:32:22 AM10/18/06
to Django users
This can be done using following command sequence:

manage.py sqlsequencereset <appname> | psql <db_name>

It sets counters to the max value of serial field of every model in
your app.

can.xiang

unread,
Oct 26, 2006, 10:22:44 PM10/26/06
to Django users
Denis, your suggestion fixed a similar problem bugging me hours.

I tried to migrate some (1,000s rows) tables from mysql to postgresql
with the build-in model functions, which is absolutely no problem.
While when I tried to add new data with manipulator.save(), it always
raise "integrity error" on the primary key.

It fixed the problem with the reset on _id_seq. Thanks!

Reply all
Reply to author
Forward
0 new messages