Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31699 closed Cleanup/optimization (duplicate)

Fields placed at the SELECT clause in an erroneous order.

Reported by: Thodoris Sotiropoulos Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a query of the form (simplified for opening issue)

q1 = Model.objects.values('col_a', 'col_b')
q2 = Model.objects.annotate(col_c=F('col_a')).values('col_c', 'col_b')
q1.union(q2)

The generated SQL query is

SELECT "model"."col_a", "model"."col_b" FROM "model" UNION SELECT "model"."col_b", "model"."col_a" AS "col_c" FROM "model"

It seems to me that when inspecting the second sub-query, django does not place fields at the SELECT clause in the correct order, that is

SELECT "model"."col_b", "model"."col_a" AS "col_c"

instead of

SELECT  "model"."col_a" AS "col_c", "model"."col_b"

Change History (3)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: wontfix
Status: newclosed
Summary: Fields placed at the SELECT clause in an erroneous orderFields placed at the SELECT clause in an erroneous order.
Type: UncategorizedCleanup/optimization

This by design, annotation are always at the end. It doesn't cause any issue in your example.

comment:2 by Thodoris Sotiropoulos, 4 years ago

Ah OK, thanks.

There is actually an issue in this example, because columns of the two sub-queries are not combined correctly.
Column 'col_c' is combined with 'column_b' (and not column 'column_a as intended).

Version 0, edited 4 years ago by Thodoris Sotiropoulos (next)

comment:3 by Mariusz Felisiak, 4 years ago

Resolution: wontfixduplicate

Ahh Yes, sorry I missed union(), so it's a duplicate of #28553.

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