﻿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
13204	`values()` returns empty list after `extra(order_by)`	Andy Durdin	nobody	"Given any model with an integer 'id' column, if you perform an `extra()` query that creates a new column and orders by it, `.values()` fails even if selecting values from a different column:
{{{
>>> qs = MyModel.objects.extra({'new_number': '-id'}, order_by=['-new_number']).values('id')
>>> list(qs)
[]
>>> qs.count()
66670
}}}
If you try to display the query, a FieldError is raised:
{{{
>>> print qs.query
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File "".../django/db/models/sql/query.py"", line 113, in __str__
    sql, params = self.as_sql()
  File "".../django/db/models/sql/query.py"", line 397, in as_sql
    ordering, ordering_group_by = self.get_ordering()
  File "".../django/db/models/sql/query.py"", line 962, in get_ordering
    self.model._meta, default_order=asc):
  File "".../django/db/models/sql/query.py"", line 991, in find_ordering_name
    opts, alias, False)
  File "".../django/db/models/sql/query.py"", line 1737, in setup_joins
    ""Choices are: %s"" % (name, "", "".join(names)))
FieldError: Cannot resolve keyword 'new_number' into field. Choices are: id, name
}}}

It is arguable whether such a query should be valid or not, but the behaviour here is inconsistent: an empty list when evaluating the queryset, getting the number of rows on count(), and a FieldError on most other operations.

This behaviour was noticed under Django 1.1"	Bug	closed	Database layer (models, ORM)	1.1	Normal	duplicate		me@… Gabriel Hurley	Accepted	0	0	0	0	0	0
