Monday, February 28, 2011

django, postgresql and eliminating duplicates

hi,

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 = datetime.date(2009,1,1)
tod = datetime.datetime.today()
while ((dte.year != tod.year) and (dte.month != tod.month+1)):
if
Scoringrecord.objects.filter(member=mem,scoredate=dte).count() > 1:
for x in
Scoringrecord.objects.filter(member=mem,scoredate=dte)[1:]:
x.delete()
dte = dte+datetime.timedelta(days=1)
return 1
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment