Opened 9 years ago

Closed 2 years ago

#24991 closed Bug (duplicate)

Range types not properly converted to SQL in QuerySet.query.__str__()

Reported by: Villiers Strauss Owned by: Dmitry Dygalo
Component: contrib.postgres Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Villiers Strauss)

The string representation of the QuerySet.query object, which is useful in debugging, does not convert range types to their SQL equivalents, instead it just displays the string representation of the range type.

>>> import datetime
>>> from django.utils.timezone import utc
>>> from psycopg.extras import NumericRange, DateRange, DateTimeTZRange
>>> from myapp.models import MyModel  # model defintion is irrelevant

>>> print(MyModel.objects.filter(num_rng__overlap=NumericRange(1, 5)).query)
SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."num_rng" && NumericRange(1, 5, '[)'))

>>> print(MyModel.objects.filter(date_rng__overlap=DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1))).query)
SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."date_rng" && DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1), '[)'))

>>> print(MyModel.objects.filter(dt_rng__overlap=DateTimeTZRange(datetime.datetime(2015, 5, 1, tzinfo=utc), datetime.datetime(2015, 6, 1, tzinfo=utc))).query)
SELECT [...] FROM "myapp_mymodel" WHERE ("myapp_mymodel"."dt_rng" && DateTimeTZRange(datetime.datetime(2015, 5, 1, 0, 0, tzinfo=<UTC>), datetime.datetime(2015, 6, 1, 0, 0, tzinfo=<UTC>), '[)'))

The expected output is numrange(1.0, 5.0), daterange('2015-05-01', '2015-06-01') and tstzrange('2015-05-01 00:00:00+00:00', '2015-06-01 00:00:00+00:00') respectively.

Change History (10)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Villiers Strauss, 9 years ago

Description: modified (diff)

comment:3 by Dmitry Dygalo, 8 years ago

Owner: set to Dmitry Dygalo
Status: newassigned

comment:4 by Dmitry Dygalo, 8 years ago

Based on psycopg2.extensions.adapt output it should be converted as:
NumericRange(1, 5) to '[1,5)'
DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1)) to daterange('2015-05-01'::date, '2015-06-01'::date, '[)')
DateTimeTZRange(datetime.datetime(2015, 5, 1, tzinfo=utc), datetime.datetime(2015, 6, 1, tzinfo=utc)) to tstzrange('2015-05-01T00:00:00+00:00'::timestamptz, '2015-06-01T00:00:00+00:00'::timestamptz, '[)')

Please correct me if I'm wrong. If it is true, then I'll try to provide PR for that.

in reply to:  4 comment:5 by Villiers Strauss, 8 years ago

Replying to Stranger6667:

Based on psycopg2.extensions.adapt output it should be converted as:
NumericRange(1, 5) to '[1,5)'
DateRange(datetime.date(2015, 5, 1), datetime.date(2015, 6, 1)) to daterange('2015-05-01'::date, '2015-06-01'::date, '[)')
DateTimeTZRange(datetime.datetime(2015, 5, 1, tzinfo=utc), datetime.datetime(2015, 6, 1, tzinfo=utc)) to tstzrange('2015-05-01T00:00:00+00:00'::timestamptz, '2015-06-01T00:00:00+00:00'::timestamptz, '[)')

Please correct me if I'm wrong. If it is true, then I'll try to provide PR for that.

That looks right. The output should ideally be exactly what gets executed in the database, to make debugging easier.

comment:6 by Tim Graham, 8 years ago

Has patch: set

Looks like #25705 provides a patch for this issue.

comment:7 by Tim Graham, 8 years ago

Patch needs improvement: set

Created a pull request with the patch from #25705 but there are some test failures.

comment:8 by Claude Paroz, 4 years ago

Patch needs improvement: unset

I tried to fix errors in that PR. I'm not very proud of those regex testing, but well, it works...

comment:9 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set
Version: 1.8master

comment:10 by Mariusz Felisiak, 2 years ago

Resolution: duplicate
Status: assignedclosed

Duplicate of #25705.

Note: See TracTickets for help on using tickets.
Back to Top