Opened 4 years ago
Last modified 4 years ago
#32627 closed Bug
Multi-model union fails with .first() — at Initial Version
Reported by: | Iuri de Silvio | Owned by: | nobody |
---|---|---|---|
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
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.
[1] 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
Note:
See TracTickets
for help on using tickets.