Monday, February 28, 2011

django, postgresql and eliminating duplicates


I have a model which allowed duplicates to be entered. Subsequently it
was decided to add a unique_together constraint and make the necessary
change in the db structure. Postgresql of course will not make a
unique_together constraint if there are duplicates. Since this is one
off, I wrote a script to eliminate duplicates, the script worked, but
psql is still not accepting the constraint saying there *are*
duplicates. The script is here:

def getdups():
mems = Member.objects.all()

for mem in mems:

dte =,1,1)
tod =
while ((dte.year != tod.year) and (dte.month != tod.month+1)):
Scoringrecord.objects.filter(member=mem,scoredate=dte).count() > 1:
for x in
dte = dte+datetime.timedelta(days=1)
return 1
Coimbatore LUG rox

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment