#32627: Multi-model union fails with .first()
-------------------------------------+-------------------------------------
Reporter: Iuri de Silvio | Owner: Iuri de
| Silvio
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
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 Iuri de Silvio):
* owner: nobody => Iuri de Silvio
* status: new => assigned
Old description:
> Django 3.2 fails with this query:
>
> {{{#!python
> 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
> 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
> 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
> }}}
New description:
Django 3.2 fails with this query:
{{{#!python
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
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
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
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32627#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.8599126a95b063035c21085130668d6e%40djangoproject.com.