[Django] #37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and TIME_ZONE != UTC creates ambiguity for migrations

6 views
Skip to first unread message

Django

unread,
Mar 30, 2026, 3:22:00 PM (9 days ago) Mar 30
to django-...@googlegroups.com
#37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and
TIME_ZONE != UTC creates ambiguity for migrations
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 6.0 | Severity: Normal
Keywords: tzinfo, TIME_ZONE | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
The `Trunc()` and `Extract()` database functions apply timezone
conversions if `USE_TZ = True`. When the `tzinfo` argument is omitted, the
timezone is inferred from `settings.TIME_ZONE`. This information is not
captured by migrations, meaning that if `settings.TIME_ZONE` changes over
the life of a project, then the database may never receive a corresponding
update.

{{{#!py
from django.db import models
from django.db.models.functions import ExtractHour, Now

class Person(models.Model):
hour = models.IntegerField(db_default=ExtractHour(Now()))
}}}

To reproduce:
- Change `settings.TIME_ZONE` to "America/Chicago".
- Make migrations, notice no migration generated.
- Emulating the database-default python-side (as SQLite must do sometimes,
see rest of linked forum post) now no longer produces the same value as
the database.

In this [https://forum.djangoproject.com/t/extract-isnt-safe-to-use-in-db-
default-on-sqlite-if-time-zone-changes-at-some-point/44781/6 forum reply],
there was an idea to implement `Trunc/Extract.deconstruct()` to deprecate
omitting `tzinfo` if serialized into a migration and eventually make it
default to `get_current_timezone()` when not provided.
--
Ticket URL: <https://code.djangoproject.com/ticket/37013>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 30, 2026, 3:22:41 PM (9 days ago) Mar 30
to django-...@googlegroups.com
#37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and
TIME_ZONE != UTC creates ambiguity for migrations
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: tzinfo, TIME_ZONE | 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 Jacob Walls:

Old description:

> The `Trunc()` and `Extract()` database functions apply timezone
> conversions if `USE_TZ = True`. When the `tzinfo` argument is omitted,
> the timezone is inferred from `settings.TIME_ZONE`. This information is
> not captured by migrations, meaning that if `settings.TIME_ZONE` changes
> over the life of a project, then the database may never receive a
> corresponding update.
>
> {{{#!py
> from django.db import models
> from django.db.models.functions import ExtractHour, Now
>
> class Person(models.Model):
> hour = models.IntegerField(db_default=ExtractHour(Now()))
> }}}
>
> To reproduce:
> - Change `settings.TIME_ZONE` to "America/Chicago".
> - Make migrations, notice no migration generated.
> - Emulating the database-default python-side (as SQLite must do
> sometimes, see rest of linked forum post) now no longer produces the same
> value as the database.
>
> In this [https://forum.djangoproject.com/t/extract-isnt-safe-to-use-in-
> db-default-on-sqlite-if-time-zone-changes-at-some-point/44781/6 forum
> reply], there was an idea to implement `Trunc/Extract.deconstruct()` to
> deprecate omitting `tzinfo` if serialized into a migration and eventually
> make it default to `get_current_timezone()` when not provided.

New description:

The `Trunc()` and `Extract()` database functions apply timezone
conversions if `USE_TZ = True`. When the `tzinfo` argument is omitted, the
timezone is inferred from `settings.TIME_ZONE`. If these functions are
used in a `db_default` expression, this information is not captured by
migrations, meaning that if `settings.TIME_ZONE` changes over the life of
a project, then the database may never receive a corresponding update.

{{{#!py
from django.db import models
from django.db.models.functions import ExtractHour, Now

class Person(models.Model):
hour = models.IntegerField(db_default=ExtractHour(Now()))
}}}

To reproduce:
- Change `settings.TIME_ZONE` to "America/Chicago".
- Make migrations, notice no migration generated.
- Emulating the database-default python-side (as SQLite must do sometimes,
see rest of linked forum post) now no longer produces the same value as
the database.

In this [https://forum.djangoproject.com/t/extract-isnt-safe-to-use-in-db-
default-on-sqlite-if-time-zone-changes-at-some-point/44781/6 forum reply],
there was an idea to implement `Trunc/Extract.deconstruct()` to deprecate
omitting `tzinfo` if serialized into a migration and eventually make it
default to `get_current_timezone()` when not provided.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/37013#comment:1>

Django

unread,
Mar 30, 2026, 3:26:18 PM (9 days ago) Mar 30
to django-...@googlegroups.com
#37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and
TIME_ZONE != UTC creates ambiguity for migrations
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: tzinfo, TIME_ZONE, | Triage Stage: Accepted
Extract, Trunc |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* keywords: tzinfo, TIME_ZONE => tzinfo, TIME_ZONE, Extract, Trunc
* stage: Unreviewed => Accepted

Comment:

Thank you! The forum post was very educational.
--
Ticket URL: <https://code.djangoproject.com/ticket/37013#comment:2>

Django

unread,
Apr 6, 2026, 2:58:41 PM (2 days ago) Apr 6
to django-...@googlegroups.com
#37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and
TIME_ZONE != UTC creates ambiguity for migrations
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Huwaiza
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: tzinfo, TIME_ZONE, | Triage Stage: Accepted
Extract, Trunc |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Huwaiza):

* cc: Huwaiza (added)
* owner: (none) => Huwaiza
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/37013#comment:3>

Django

unread,
Apr 6, 2026, 3:29:33 PM (2 days ago) Apr 6
to django-...@googlegroups.com
#37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and
TIME_ZONE != UTC creates ambiguity for migrations
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Huwaiza
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: tzinfo, TIME_ZONE, | Triage Stage: Accepted
Extract, Trunc |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Huwaiza):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37013#comment:4>

Django

unread,
Apr 6, 2026, 3:51:13 PM (2 days ago) Apr 6
to django-...@googlegroups.com
#37013: Omitting tzinfo argument to Trunc & Extract with USE_TZ = True and
TIME_ZONE != UTC creates ambiguity for migrations
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Huwaiza
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: tzinfo, TIME_ZONE, | Triage Stage: Accepted
Extract, Trunc |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/21064 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/37013#comment:5>
Reply all
Reply to author
Forward
0 new messages