[ACCEPTED]-get_or_create failure with Django and Postgres (duplicate key value violates unique constraint)-postgresql
Another possible reason for these errors 6 in get_or_create()
is data type mismatch in one of the 5 search fields - for example passing False
instead 4 of None
into a nullable field. The .get()
inside .get_or_create()
will 3 not find it and Django will continue with 2 new row creation - which will fail due to 1 PostgreSQL constraints.
I had issues with get_or_create when using 2 postgres. In the end I abandoned the boilerplate 1 code for traditional:
try:
jobInvite = Invite.objects.get(sender=employer.user, job=job)
except Invite.DoesNotExist:
jobInvite = Invite(sender=employer.user, job=job)
jobInvite.save()
# end try
Have you at some point had unique=True set 9 on Visit's profile field?
It looks like there's 8 been a unique constraint generated for postgres 7 that's still in effect. "table_visit_profile_id_key" is 6 what it's auto generated name would be, and 5 naturally it would cause those errors if 4 you're recording multiple visits for a user.
If 3 this is the case, are you using South to manage 2 your database changes? If you aren't, grab 1 it!
PostgreSQL behaves somewhat differently 5 in some subtle queries, which results in 4 IntegrityError
errors, especially after you switch to 3 Django 1.6. Here's the solution - you need to add 2 select_on_save
option to each failing model:
class MyModel(models.Model):
...
class Meta:
select_on_save = True
It's documented 1 here: Options.select_on_save
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.