Opened 3 weeks ago

Last modified 3 weeks ago

#37014 assigned Bug

SQLite emulation of db_default involving Extract/Trunc might unexpectedly apply overridden timezone for inserted values

Reported by: Jacob Walls Owned by: Praful Gulani
Component: Database layer (models, ORM) Version: 6.0
Severity: Normal Keywords: tzinfo, TIME_ZONE, Extract, Trunc
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As developed in this forum post, if

  • USE_TZ = True
  • a model field uses Extract or Trunc in a db_default expression
  • a test case uses override_settings() to test timezone behavior

... then in scenarios where Django must emulate the database default for SQLite in Python, the value will be transformed according to the overridden timezone setting, but this is an improper emulation, since the database's default cannot be overridden by merely flipping a Django setting.

The scenarios where Django must emulate the db default for SQLite vary depending on edges like:

  • whether any field's value has been set
  • whether the field with Extract/Trunc is last
  • possibly: whether create() vs. bulk_create() is called

There are two ways to fix this as described on forum:

  • introduce a heuristic (Simon: if ddl_compiler_heuristic():) to switch on whether to use the project default timezone or the test's overridden one
  • implement supports_default_keyword_in_insert on SQLite by emulating it in its insert compiler

Having only thought about it today, the second approach seems like it would be simpler to implement and potentially pay broader dividends for other use cases.

Related ticket: #37013

Change History (2)

comment:1 by Simon Charette, 3 weeks ago

Triage Stage: UnreviewedAccepted

I also think the second solution is the preferable one as there are likely other expressions out there that rely on global state expected to be captured at field definition time and might suffer from the same problem. It also more aligned with how we've been doing things historically when running into these kind of problems.

comment:2 by Praful Gulani, 3 weeks ago

Owner: set to Praful Gulani
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top