Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#35111 closed Bug (fixed)

DateField's __range and __in lookups against expressions produce invalid values on SQLite.

Reported by: Simon Charette Owned by: Simon Charette
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

#22288 (4f138fe5a496a81115c4fba6615a517fc62c3b17) only partially addressed the issue of iterable right-hand-side lookups (__range and __in) by adapting Oracle's and SQLite operation modules to special case the adaptation of datetime and time value while missing that date values are still converted to string.

This causes queries of the form

Person.objects.filter(
    dob__between=(F("dob"), F("dob"))
)

To be turned into

SELECT * FROM person WHERE dob BETWEEN "Col('dob')" AND "Col('dob')"

I believe the fundamental flaw in 4f138fe5a496a81115c4fba6615a517fc62c3b17 is that expressions (hasattr(obj, "resolve_expression")) should never be passed to Field.get_db_prep_value in the first place during the Lookup's right-hand-side processing phase just like they are not passed to Field.get_prep_value during the preparation phase.

Initially discovered on the forum by Joel Feiner.

Change History (7)

comment:1 by Simon Charette, 5 months ago

Has patch: set

comment:2 by Mariusz Felisiak, 5 months ago

Owner: changed from nobody to Simon Charette
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:3 by Mariusz Felisiak, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In 0fcee16:

Fixed #35111 -- Fixed compilation of DateField in/range rhs on SQLite and MySQL.

Also removed tests that ensured that adapt_(date)timefield backend
operations where able to deal with expressions when it's not the case
for any other adapt methods.

comment:5 by Claude Paroz, 5 months ago

Does this has any effect on #25591?

comment:6 by Simon Charette, 5 months ago

From a quick look I don't think it does Claude as the logic changed here only relates to lookups. In other words, RangeField and RangeLookup are completely different beasts.

comment:7 by Claude Paroz, 5 months ago

OK, thanks Simon and sorry for the noise.

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