#31566 closed Bug (fixed)
Subquery in annotation is included by its alias and is not present in the SELECT
Reported by: | Gagaro | Owned by: | Simon Charette |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.0 |
Severity: | Release blocker | 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
With the following queryset:
IndicatorValue.objects .values("freight") .annotate( has_top_loading=Exists(OrderItemResult.objects.order_by().filter(order_line__order__freight=OuterRef("freight"), loading_arm__loading_type=LoadingArm.LOADING_TYPE_TOP,).values('pk')), start_time=Min("datetime") ) .values('start_time')
I have the following error:
ProgrammingError: column "has_top_loading" does not exist LINE 1: ...ROUP BY "indicators_indicatorvalue"."freight_id", "has_top_l...
The generated SQL is:
SELECT MIN("indicators_indicatorvalue"."datetime") AS "start_time" FROM "indicators_indicatorvalue" GROUP BY "indicators_indicatorvalue"."freight_id", "has_top_loading"
has_top_loading
is only mentioned in the GROUP_BY
and not in the SELECT
.
On Django 2.2, the same queryset generates the following SQL:
SELECT MIN("indicators_indicatorvalue"."datetime") AS "start_time" FROM "indicators_indicatorvalue" GROUP BY "indicators_indicatorvalue"."freight_id", (EXISTS(SELECT U0."id" FROM "orders_orderitemresult" U0 INNER JOIN "loading_terminal_loadingarm" U1 ON (U0."loading_arm_id" = U1."id") INNER JOIN "orders_orderitem" U2 ON (U0."order_line_id" = U2."id") INNER JOIN "orders_order" U3 ON (U2."order_id" = U3."id") WHERE (U1."loading_type" = TOP AND U3."freight_id" = ("indicators_indicatorvalue"."freight_id"))))
Change History (11)
comment:1 by , 5 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 5 years ago
Out of curiosity, does the error happens against 3.0.6 as well and can you also reproduce by breaking your .annotate
call in two: a first one for the start_time=Min("datetime")
and a following one for the Exists
?
comment:4 by , 5 years ago
Simon, I can reproduce this at 46fe506445666d8097945f0c1e8be11cfd644b28, also with two annotate()
calls.
comment:5 by , 5 years ago
I had tested it on 3.0.6 and stable/3.0.x branch. I didn't try it with two annotate
but felixxm just did.
Thanks for taking care of it so quickly.
comment:8 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
This is similar #31377, we'll want to ensure the alias is part of
.annotation_select
.https://github.com/django/django/blob/d6aff369ad33457ae2355b5b210faf1c4890ff35/django/db/models/sql/query.py#L1976-L1977