#26019: Incorrect query generated for QuerySet Union with annotate
----------------------------------------------+--------------------
Reporter: mssnlayam | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
The Django ORM generates incorrect queries for a QuerySet union that has
an annotate. Here's output from a Python shell session illustrating this
error.
I am trying to do a union of two QuerySets, one which gets the first_name
field (renamed as name_field), and another one which gets the last_name
field (also renamed as name_field). The unioned QuerySet gets only the
first_name field values.
{{{
>>> from django.contrib.auth.models import User
>>> from django.db.models import F
>>> first_name_qs =
User.objects.order_by().annotate(name_field=F('first_name')).values('name_field').distinct()
>>> last_name_qs =
User.objects.order_by().annotate(name_field=F('last_name')).values('name_field').distinct()
>>> print(first_name_qs.query)
SELECT DISTINCT "auth_user"."first_name" AS "name_field" FROM "auth_user"
>>> print(last_name_qs.query)
SELECT DISTINCT "auth_user"."last_name" AS "name_field" FROM "auth_user"
>>> print((first_name_qs | last_name_qs).query)
SELECT DISTINCT "auth_user"."first_name" AS "name_field" FROM "auth_user"
}}}
The above behavior is with Django 1.9.
--
Ticket URL: <https://code.djangoproject.com/ticket/26019>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.d0397ac009652bc036af52edb479f749%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.