[Django] #31699: Fields placed at the SELECT clause in an erroneous order

2 views
Skip to first unread message

Django

unread,
Jun 12, 2020, 7:59:54 AM6/12/20
to django-...@googlegroups.com
#31699: Fields placed at the SELECT clause in an erroneous order
-------------------------------------------------+------------------------
Reporter: Thodoris Sotiropoulos | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------------+------------------------
I have a query of the form (simplified for opening issue)

{{{#!python
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

{{{#!sql
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

{{{#!sql
SELECT "model"."col_b", "model"."col_a" AS "col_c"
}}}

instead of

{{{#!sql
SELECT "model"."col_a" AS "col_c", "model"."col_b"
}}}

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

Django

unread,
Jun 12, 2020, 9:14:42 AM6/12/20
to django-...@googlegroups.com
#31699: Fields placed at the SELECT clause in an erroneous order.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: nobody
Sotiropoulos |
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => wontfix
* type: Uncategorized => Cleanup/optimization
* component: Uncategorized => Database layer (models, ORM)


Comment:

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

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

Django

unread,
Jun 12, 2020, 9:20:47 AM6/12/20
to django-...@googlegroups.com
#31699: Fields placed at the SELECT clause in an erroneous order.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: nobody
Sotiropoulos |
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Thodoris Sotiropoulos):

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

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

Django

unread,
Jun 12, 2020, 9:46:04 AM6/12/20
to django-...@googlegroups.com
#31699: Fields placed at the SELECT clause in an erroneous order.
-------------------------------------+-------------------------------------
Reporter: Thodoris | Owner: nobody
Sotiropoulos |
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* resolution: wontfix => duplicate


Comment:

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

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

Reply all
Reply to author
Forward
0 new messages