#36876: Model timestamp fields get different values using auto_now
-------------------------------------+-------------------------------------
     Reporter:  thomax               |                     Type:  Bug
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.2                  |                 Severity:  Normal
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 I have a BaseModel, which I use to set created_at and updated_at. Like so:

 {{{
 class BaseModel(models.Model):
     id = models.CharField(primary_key=True, max_length=50,
 default=generate_nanoid, editable=False)
     created_at = models.DateTimeField(auto_now_add=True)
     updated_at = models.DateTimeField(auto_now=True)
 }}}

 When a row is created in one of my tables, both created_at and udpated_at
 fields are set. Which is expected. What is ''not'' expected, is that the
 created_at field gets a slightly older value than the updated_at field.
 Typically:

 created_at: 2026-01-22 08:53:38.479806+00
 updated_at: 2026-01-22 08:53:38.479828+00

 This is, in my opinion, a bug. Because create happens (I presume) in a
 single transaction. And also because these fields when, compared for
 similiarty, should reveal if the row has been updated at some later time.

 As a workaround, one could override the model save method like so:

 {{{
     def save(self, *args, **kwargs):
         """Ensure created_at and updated_at are identical on creation"""
         if self._state.adding:
             now = timezone.now()
             if not self.created_at:
                 self.created_at = now
             self.updated_at = self.created_at
         super().save(*args, **kwargs)
 }}}

 This should probably get attention because it might be a symptom of
 inconsistent timestamp usage during create?

 (My exact django version is v5.2.9)
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36876>
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019be51dec76-9587e47d-9d7e-403d-985f-b44cc7a815c9-000000%40eu-central-1.amazonses.com.

Reply via email to