title: get_or_create failure with Django and Postgres (duplicate key value violates unique constraint)
body: <p>Thanks for taking time to read my question.</p>
  
  <p>I have a django app with the following model:</p>
  
  <pre><code>class UserProfile(models.Model):
      user = models.OneToOneField(User)
      ...
  
  class Visit(models.Model):
      profile = models.ForeignKey(UserProfile)
      date = models.DateField(auto_now_add=True, db_index=True)
      ip = models.IPAddressField()
      class Meta:
          unique_together = ('profile', 'date', 'ip')
  </code></pre>
  
  <p>In a view:</p>
  
  <pre><code>profile = get_object_or_404(Profile, pk = ...)
  get, create = Visit.objects.get_or_create(profile=profile, date=now.date(), ip=request.META['REMOTE_ADDR'])
  if create: DO SOMETHING
  </code></pre>
  
  <p>Everything works fine, except that the Postgres Logs are full with duplicate key errors:</p>
  
  <pre><code>2012-02-15 14:13:44 CET ERROR:  duplicate key value violates unique constraint "table_visit_profile_id_key"
  2012-02-15 14:13:44 CET STATEMENT:  INSERT INTO "table_visit" ("profile_id", "date", "ip") VALUES (1111, E'2012-02-15', E'xx.xx.xxx.xxx') RETURNING "table_visit"."id"
  </code></pre>
  
  <p>Tried different solution e.g.</p>
  
  <pre><code>from django.db import transaction 
  from django.db import IntegrityError
  
  @transaction.commit_on_success
  def my_get_or_create(prof, ip):    
      try:
          object = Visit.objects.create(profile=prof, date=datetime.now().date(), ip=ip)
      except IntegrityError:
          transaction.commit()
          object = Visit.objects.get(profile=prof, date=datetime.now().date(), ip=ip)
      return object
  
  ....
  
  created = my_get_or_create(prof, request.META['REMOTE_ADDR'])
  if created: DO SOMETHING
  </code></pre>
  
  <p>This only helps for MySQL? Does anyone know how to avaid the duplicate key value errors for postgres?</p>