﻿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
23259	Query extra() select_params are inserted in the incorrect order in the overall query parameters with values_list	rajivm	Anssi Kääriäinen	"This is running the latest stable/1.7 (267630ad50c69ebfe594de37a0636264aa5be7d6)

{{{
User.objects.extra(select=OrderedDict([('points', 'id + %s')]), select_params=[5]).filter(id__gte=3).extra(order_by=['points']).values_list(""id"", flat=True)
}}}

{{{
[DEBUG django.db.backends] (0.001) SELECT ""auth_user"".""id"" FROM ""auth_user"" WHERE ""auth_user"".""id"" >= 5 ORDER BY (id + 3) ASC LIMIT 21; args=(5, 3)
}}}

Whereas in Django 1.6 (the correct query):

{{{
[DEBUG django.db.backends] (0.003) SELECT ""auth_user"".""id"", (id + 5) AS ""points"" FROM ""auth_user"" WHERE ""auth_user"".""id"" >= 3  ORDER BY ""points"" ASC LIMIT 21; args=(5, 3)
}}}

In the second case, the extra select_param of 5 is correctly matched with the extra select ""id + %s"", resulting in id+5.
However, in the first case, in Django 1.7, the extra select results in id + 3 (incorrectly inverting the parameters to the query).

It seems like what is happening is the ""select"" of ""points"" (the extra select) is being optimized out because of the values_list, but is being kept by the extra(order_by) -- resulting in it being in a different part of the query.

I tried to simplify the example as much as possible (I had a much crazier query this was destroying).

Let me know if you have any questions."	Bug	closed	Database layer (models, ORM)	1.7-rc-2	Release blocker	fixed			Accepted	1	0	1	1	0	0
