#36876: Model timestamp fields get different values using auto_now
-------------------------------------+-------------------------------------
     Reporter:  Thomas Drevon        |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Thomas Drevon:

Old description:

> 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)

New description:

 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"""
     now = timezone.now()
     if self._state.adding:
         # On creation, set both to the same timestamp
         if not self.created_at:
             self.created_at = now
         self.updated_at = self.created_at
     else:
         # On update, only touch updated_at
         self.updated_at = now
     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#comment:1>
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/0107019be5224228-cb8be768-f035-4df2-a322-630633a88b3c-000000%40eu-central-1.amazonses.com.

Reply via email to