#36025: __range lookup in conditional aggregate with subquery annotation does not
use annotated related fields
-------------------------------------+-------------------------------------
Reporter: Aashay.Amballi | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ORM | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* cc: Simon Charette (added)
Comment:
An important lead here is likely this statement
> Explicitly using `__gte` and `__lte` instead of using `__range` resolved
the issue.
If that's truly the case then it means that the likely culprit is how
`RangeLookup` stores its resolved right-hand-side as a `tuple(Col(...),
Col(...))` which trips resolvable detection (`hasattr(expr,
"resolve_expression")`) that happens when alias relabeling takes place
(see `Expression.relabeled_clone`).
`Expression.relabeled_clone`, which is called when subqueries are involved
and tables need to be re-aliases to avoid conflicts, relies on
`self.get_source_expression` to ''walk'' through the expression tree and
make sure all nested references to tables are re-aliases. If you look at
`Lookup.get_source_expressions` though you'll notice that the right-hand-
side is excluded if it's not a ''compilable'' (this should likely be a
''resolvable'' check instead). Since `RangeLookup.rhs` is a `tuple` and
thus not considered a ''resolvable'' it falls on its face and isn't
considered for re-labeling.
We have a larger problem here where lookups that allow containers of
potentially ''resolvable'' members as right-hand-side (e.g. `__range`,
`__in`) that are not considered as ''resolvable'' themselves that we
should address but the immediate solution for `RangeLookup` appears to be
wrapping it's right-hand-side in an `ExpressionList` at resolving time.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36025#comment:4>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.