#22571: DateTimeField(auto_now_add=True) Breaks
-------------------------+-------------------------------------------------
     Reporter:           |      Owner:  nobody
  nu.everest@…           |     Status:  new
         Type:           |    Version:  1.4
  Uncategorized          |   Keywords:  integrityerror auto_now_add
    Component:           |  get_or_create duplicatekey
  Uncategorized          |  Has patch:  0
     Severity:  Normal   |      UI/UX:  0
 Triage Stage:           |
  Unreviewed             |
Easy pickings:  0        |
-------------------------+-------------------------------------------------
 Example:
 # Given this simple model
 class Foo(models.Model):
     name = models.CharField(max_length=100)
     date_added = models.DateTimeField(auto_now_add=True)

 # This will always be true, even if an instance
 # with this name and today's date already exists

 bar, created = Foo.objects.get_or_create(
     name = 'Alex',
     date_added = some_datetime_obj
 )

 print created
 # >> True


 # The problem is, auto_now_add does some stuff that
 # makes it uneditable, and messes up my expectations
 # when using it with get_or_create

 # Here's the solution
 class Foo(models.Model):
     name = models.CharField(max_length=100)
     date_added = models.DateTimeField(default=datetime.today())

 bar, created = Foo.objects.get_or_create(
     name = 'Alex',
     date_added = some_datetime_obj
 )

 print created
 # >> False

 Error:
 django.db.utils.IntegrityError: duplicate key value violates unique
 constraint


 These two links document the issue:
 http://alexkehayias.tumblr.com/post/33889961953/django-gotcha-duplicate-
 models-when-using

 http://stackoverflow.com/questions/9297422/get-or-create-failure-with-
 django-and-postgres-duplicate-key-value-violates-uni

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22571>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.15e266088f142f4bfb7927fe4908c9c9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to