[Django] #35193: Timezone unaware datetimes and psycopg 3

2 views
Skip to first unread message

Django

unread,
Feb 13, 2024, 11:07:59 AMFeb 13
to django-...@googlegroups.com
#35193: Timezone unaware datetimes and psycopg 3
-------------------------------------+-------------------------------------
Reporter: Mapiarz | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi. I encountered a blocker that I'm not sure how to resolve.

I'm running Django 4.2 and wanted to update from psycopg2 to psycopg (the
latest version 3).

The update itself is quite simple in my case and almost everything works
except timezone unaware datetime ranges. Let me explain.

In my app, I have timezones enabled. For a few rare exceptions, I need to
store timezone unaware datetimes. I was able to accomplish this by
subclassing the DateTimeField shipped by Django and removing the related
timezone conversions. Furthermore, I also need timezone unaware datetime
ranges. That's also easy since I can subclass DateTimeRangeField and
specify my timezone unaware field as the base_field, use timezone unaware
range_type and make sure the correct db_type is returned from db_type
method (tsrange).

This has gotten me a very long way with psycopg2. After migrating to
latest psycopg, I started getting a DB crash whenever I try to modify one
of my objects that has a timezone unaware datetime range. This crash,
specifically, happens when checking an ExclusionConstraint with an
OVERLAPS expression on the datetime range. The error is:

Got a database error calling check() on ...: cannot cast type tstzrange to
tsrange ...02-17 00:00:00","2024-02-24 00:00:00")'::tstzrange::tsrange)...

As far as I can tell, this is caused by
django.db.backends.postgresql.psycopg_any.DjangoRangeDumper which assumes
that there can only ever be timezone aware timestamps.

Overall, I'm not sure how to proceed. I'm not familiar with the django
postgres backend or psycopg itself. Please advise.
--
Ticket URL: <https://code.djangoproject.com/ticket/35193>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Reply all
Reply to author
Forward
0 new messages