﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26019	Incorrect query generated when combining querysets refering to different fields under the same alias.	Suriya Subramanian	nobody	"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.
"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		Simon Charette	Accepted	0	0	0	0	0	0
