Opened 4 years ago
Last modified 4 years ago
#33225 assigned Bug
Missing FROM-clause when ordering by a relationship defined on the parent model in a Subquery inside a negated Q
| Reported by: | InvalidInterrupt | Owned by: | Geonsang Yoo |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | InvalidInterrupt | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I encountered the following error in 3.2, and produced this test case against main (551c997f) today; using the postgres backend for all tests.
Using the models from tests.model_inheritance_regress.models, when running:
list(
ItalianRestaurant.objects.filter(
~Q(
supplier__pk__in=Subquery(
ItalianRestaurant.objects.filter(pk=OuterRef('pk'))
.order_by('serves_gnocchi')
.values('pk')
)
)
)
)
ProgrammingError is raised, due to a missing JOIN of model_inheritance_regress_restaurant.
This is as much as I was able to reduce the test case. In particular, the following seem necessary to encounter the error:
- The negation of the Q object
- The
__inlookup passing through a relationship defined on the parent model - The
filter()on anOuterRef - The use of
order_by()(distinct(*fields)may be substituted)
The error also occurs if values('supplier__pk') is used as would be semantically correct.
When debugging, it appeared that the inner Query object had a refcount of zero for the alias in question, but I may have misunderstood what was happening there.
Change History (4)
comment:1 by , 4 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 4 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 4 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Thank you both (@InvalidInterrupt, @ Simon Charette) for your report and analysis!
I will try to solve this issue!!
comment:4 by , 4 years ago
| Cc: | added |
|---|
Thanks for the report.
I managed to reproduce against all versions down to 2.2 so this isn't a recent regression. It seems to be an issue with
sql.Query.split_exclude/.trim_startasquerystill has its requiredJOIN model_inheritance_regress_restaurant V1prior to calling.trim_start.