[Django] #33225: Missing FROM-clause when ordering by a relationship defined on the parent model in a Subquery inside a negated Q

14 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Django

ungelesen,
23.10.2021, 22:32:3623.10.21
an django-...@googlegroups.com
#33225: Missing FROM-clause when ordering by a relationship defined on the parent
model in a Subquery inside a negated Q
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
InvalidInterrupt |
Type: Bug | Status: new
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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 `__in` lookup passing through a relationship defined on the parent
model
* The `filter()` on an `OuterRef`
* The use of `order_by()`

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33225>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

ungelesen,
23.10.2021, 22:49:0123.10.21
an django-...@googlegroups.com
#33225: Missing FROM-clause when ordering by a relationship defined on the parent
model in a Subquery inside a negated Q
-------------------------------------+-------------------------------------
Reporter: InvalidInterrupt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | 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 InvalidInterrupt:

Old description:

New description:

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 `__in` lookup passing through a relationship defined on the parent
model
* The `filter()` on an `OuterRef`

* 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.

--

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

Django

ungelesen,
24.10.2021, 13:24:0124.10.21
an django-...@googlegroups.com
#33225: Missing FROM-clause when ordering by a relationship defined on the parent
model in a Subquery inside a negated Q
-------------------------------------+-------------------------------------
Reporter: InvalidInterrupt | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | 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):

* stage: Unreviewed => Accepted


Comment:

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_start` as `query` still has its required
`JOIN model_inheritance_regress_restaurant V1` prior to calling
`.trim_start`.

--
Ticket URL: <https://code.djangoproject.com/ticket/33225#comment:2>

Django

ungelesen,
25.10.2021, 03:43:3325.10.21
an django-...@googlegroups.com
#33225: Missing FROM-clause when ordering by a relationship defined on the parent
model in a Subquery inside a negated Q
-------------------------------------+-------------------------------------
Reporter: InvalidInterrupt | Owner: Geonsang
| Yoo
Type: Bug | Status: assigned

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Geonsang Yoo):

* owner: nobody => Geonsang Yoo
* status: new => assigned


Comment:

Thank you both (@InvalidInterrupt, @ Simon Charette) for your report and
analysis!
I will try to solve this issue!!

--
Ticket URL: <https://code.djangoproject.com/ticket/33225#comment:3>

Django

ungelesen,
28.10.2021, 23:39:5828.10.21
an django-...@googlegroups.com
#33225: Missing FROM-clause when ordering by a relationship defined on the parent
model in a Subquery inside a negated Q
-------------------------------------+-------------------------------------
Reporter: InvalidInterrupt | Owner: Geonsang
| Yoo
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by InvalidInterrupt):

* cc: InvalidInterrupt (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/33225#comment:4>

Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten