[Django] #36078: Postgres date and time range fields change after saving object

7 views
Skip to first unread message

Django

unread,
Jan 9, 2025, 9:59:37 AM1/9/25
to django-...@googlegroups.com
#36078: Postgres date and time range fields change after saving object
--------------------------+--------------------------------------------
Reporter: Mapiarz | Type: Bug
Status: new | Component: contrib.postgres
Version: 4.2 | Severity: Normal
Keywords: postgres | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------+--------------------------------------------
This short test exemplifies the problem:


{{{
now = timezone.now()
empty_range = psycopg2_range.DateTimeTZRange(empty=True)

test_model = TestModel(range=psycopg2_range.DateTimeTZRange(now, now))

self.assertFalse(test_model.range.isempty)
self.assertNotEqual(test_model.range, empty_range)

test_model.save()
test_model.refresh_from_db()

self.assertTrue(test_model.range.isempty)
self.assertEqual(test_model.range, empty_range)
}}}

I would expect the field value not to change after saving, and it should
continue to have the lower, upper and boundary fields set.

Repro project:
https://github.com/Mapiarz/django_postgres_range_repro/tree/master
--
Ticket URL: <https://code.djangoproject.com/ticket/36078>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 9, 2025, 10:00:28 AM1/9/25
to django-...@googlegroups.com
#36078: Postgres date and time range fields change after saving object
----------------------------------+--------------------------------------
Reporter: Mapiarz | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 4.2
Severity: Normal | Resolution:
Keywords: postgres | 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 Mapiarz:

Old description:

> This short test exemplifies the problem:
>

> {{{
> now = timezone.now()
> empty_range = psycopg2_range.DateTimeTZRange(empty=True)
>
> test_model = TestModel(range=psycopg2_range.DateTimeTZRange(now, now))
>
> self.assertFalse(test_model.range.isempty)
> self.assertNotEqual(test_model.range, empty_range)
>
> test_model.save()
> test_model.refresh_from_db()
>
> self.assertTrue(test_model.range.isempty)
> self.assertEqual(test_model.range, empty_range)
> }}}
>
> I would expect the field value not to change after saving, and it should
> continue to have the lower, upper and boundary fields set.
>
> Repro project:
> https://github.com/Mapiarz/django_postgres_range_repro/tree/master

New description:

This short test exemplifies the problem:


{{{
now = timezone.now()
empty_range = psycopg2_range.DateTimeTZRange(empty=True)

test_model = TestModel(range=psycopg2_range.DateTimeTZRange(now, now))

self.assertFalse(test_model.range.isempty)
self.assertNotEqual(test_model.range, empty_range)

test_model.save()
test_model.refresh_from_db()

self.assertTrue(test_model.range.isempty)
self.assertEqual(test_model.range, empty_range)
}}}

I would expect the field value not to change after saving, and it should
continue to have the lower, upper and boundary fields set.

Repro project:
https://github.com/Mapiarz/django_postgres_range_repro/tree/master

The example is for DateTimeRange, but DateRange Is also affected. Other
ranges possibly too, I haven't tested.

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

Django

unread,
Jan 10, 2025, 5:25:17 AM1/10/25
to django-...@googlegroups.com
#36078: Postgres date and time range fields change after saving object
----------------------------------+--------------------------------------
Reporter: Mapiarz | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.2
Severity: Normal | Resolution: invalid
Keywords: postgres | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => invalid
* status: new => closed

Comment:

Thank you for the careful report I was able to replicate and had a look
into this

From what I can tell, this is Postgres behavior and so I don't think this
is a bug in Django
See https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-IO

> {{{
> -- includes no points (and will be normalized to 'empty')
> SELECT '[4,4)'::int4range;
> }}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36078#comment:2>

Django

unread,
Jan 10, 2025, 6:29:08 AM1/10/25
to django-...@googlegroups.com
#36078: Postgres date and time range fields change after saving object
----------------------------------+--------------------------------------
Reporter: Mapiarz | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.2
Severity: Normal | Resolution: invalid
Keywords: postgres | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Comment (by Claude Paroz):

Read also https://forum.djangoproject.com/t/baserangefield-and-equal-
lower-upper-values/35160

I think this should be at least documented.
--
Ticket URL: <https://code.djangoproject.com/ticket/36078#comment:3>
Reply all
Reply to author
Forward
0 new messages