﻿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
31699	Fields placed at the SELECT clause in an erroneous order.	Thodoris Sotiropoulos	nobody	"I have a query of the form (simplified for opening issue)

{{{#!python
q1 = Model.objects.values('col_a', 'col_b')
q2 = Model.objects.annotate(col_c=F('col_a')).values('col_c', 'col_b')
q1.union(q2)
}}}

The generated SQL query is

{{{#!sql
SELECT ""model"".""col_a"", ""model"".""col_b"" FROM ""model"" UNION SELECT ""model"".""col_b"", ""model"".""col_a"" AS ""col_c"" FROM ""model""
}}}

It seems to me that when inspecting the second sub-query, django does not place fields at the SELECT clause in the correct order, that is

{{{#!sql
SELECT ""model"".""col_b"", ""model"".""col_a"" AS ""col_c""
}}}

instead of 

{{{#!sql
SELECT  ""model"".""col_a"" AS ""col_c"", ""model"".""col_b""
}}}"	Cleanup/optimization	closed	Database layer (models, ORM)	3.0	Normal	duplicate			Unreviewed	0	0	0	0	0	0
