#32627 closed Bug (fixed)
QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns.
| Reported by: | Iuri de Silvio | Owned by: | Iuri de Silvio |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.2 |
| Severity: | Release blocker | Keywords: | |
| Cc: | David Wobrock | 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 (last modified by )
Django 3.2 fails with this query:
qs1 = Celebrity.objects.all() qs2 = ReservedName.objects.all() qs1.union(qs2).values_list('name').first()
It worked until Django 3.1.8. This commit[1] to be exactly. https://github.com/django/django/commit/464a4c0c59277056b5d3c1132ac1b4c6085aee08
This is the broken generated query. In the second query, it fetches from the first table.
SQL SELECT "queries_celebrity"."name", "queries_celebrity"."id" AS "__orderbycol2" FROM "queries_celebrity" UNION SELECT "queries_reservedname"."name", "queries_celebrity"."id" AS "__orderbycol2" -- HERE IS THE PROBLEM FROM "queries_reservedname" ORDER BY (2) ASC LIMIT 1
Before, it was:
SQL SELECT "queries_celebrity"."name", "queries_celebrity"."id" FROM "queries_celebrity" UNION SELECT "queries_reservedname"."name", "queries_reservedname"."id" FROM "queries_reservedname" ORDER BY (2) ASC LIMIT 1
Change History (7)
comment:1 by , 5 years ago
| Description: | modified (diff) |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:2 by , 5 years ago
comment:3 by , 5 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
| Patch needs improvement: | set |
| Severity: | Normal → Release blocker |
| Triage Stage: | Unreviewed → Accepted |
Thanks for this report.
Regression in 464a4c0c59277056b5d3c1132ac1b4c6085aee08.
Reproduced at 1c004939d5dd781fc16a8b4fb9696661cf545929.
comment:4 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:5 by , 5 years ago
| Summary: | Multi-model union fails with .first() → QuerySet.values()/values_list() crash on combined querysets ordered by unannotated columns. |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
I did a hacky patch just to make it work with minimal changes. https://github.com/django/django/pull/14241/commits/9d49fbfee7053c0a17108fe7a908b8956a6181dc
It was easier for me to understand the fix, but I'll cleanup the code.