﻿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
33767	Ordering by F-expression resolving to a number returns wrong results	Florian Apolloner	Florian Apolloner	"{{{
Article.objects.annotate(test=Value(42)).order_by(F(""test"").asc())
}}}
yields
{{{
Traceback (most recent call last):
  File ""/home/florian/sources/django.git/django/db/backends/utils.py"", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InvalidColumnReference: ORDER BY position 42 is not in select list
LINE 1: ...e"", 42 AS ""test"" FROM ""ordering_article"" ORDER BY 42 ASC LIM...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ""/home/florian/sources/django.git/tests/ordering/tests.py"", line 485, in test_order_by_f_expression_alias
    print(qs)
  File ""/home/florian/sources/django.git/django/db/models/query.py"", line 370, in __repr__
    data = list(self[: REPR_OUTPUT_SIZE + 1])
  File ""/home/florian/sources/django.git/django/db/models/query.py"", line 376, in __len__
    self._fetch_all()
  File ""/home/florian/sources/django.git/django/db/models/query.py"", line 1841, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File ""/home/florian/sources/django.git/django/db/models/query.py"", line 87, in __iter__
    results = compiler.execute_sql(
  File ""/home/florian/sources/django.git/django/db/models/sql/compiler.py"", line 1401, in execute_sql
    cursor.execute(sql, params)
  File ""/home/florian/sources/django.git/django/db/backends/utils.py"", line 103, in execute
    return super().execute(sql, params)
  File ""/home/florian/sources/django.git/django/db/backends/utils.py"", line 67, in execute
    return self._execute_with_wrappers(
  File ""/home/florian/sources/django.git/django/db/backends/utils.py"", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/home/florian/sources/django.git/django/db/backends/utils.py"", line 84, in _execute
    with self.db.wrap_database_errors:
  File ""/home/florian/sources/django.git/django/db/utils.py"", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File ""/home/florian/sources/django.git/django/db/backends/utils.py"", line 89, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ORDER BY position 42 is not in select list
LINE 1: ...e"", 42 AS ""test"" FROM ""ordering_article"" ORDER BY 42 ASC LIM...
}}}

the issue here is that the `F` expression is not resolved to a `Ref` like it should be "	Bug	closed	Database layer (models, ORM)	dev	Normal	duplicate		Simon Charette	Accepted	1	0	0	1	0	0
