Opened 4 years ago

Last modified 4 years ago

#32627 closed Bug

Multi-model union fails with .first() — at Version 1

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 Iuri de Silvio)

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 (1)

comment:1 by Iuri de Silvio, 4 years ago

Description: modified (diff)
Owner: changed from nobody to Iuri de Silvio
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top