summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-05-21 20:10:24 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-05-31 07:38:48 +0200
commitf6075fb333bae29ee213b050e91eaadef75496dd (patch)
treeaf294fb3cefca5edc538fa3458963dbe9894cdbc /django/db/models/sql
parentcc80979f011c72f8b4b5f35a5a36f049bc07bf0e (diff)
Fixed #26192 -- Fixed crash of ordering by constants on PostgreSQL.
Thanks Simon Charette for the review.
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/compiler.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 7fdf551554..a44adfc760 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -5,7 +5,8 @@ from itertools import chain
from django.core.exceptions import EmptyResultSet, FieldError
from django.db.models.constants import LOOKUP_SEP
-from django.db.models.expressions import OrderBy, Random, RawSQL, Ref
+from django.db.models.expressions import OrderBy, Random, RawSQL, Ref, Value
+from django.db.models.functions import Cast
from django.db.models.query_utils import QueryWrapper, select_related_descend
from django.db.models.sql.constants import (
CURSOR, GET_ITERATOR_CHUNK_SIZE, MULTI, NO_RESULTS, ORDER_DIR, SINGLE,
@@ -278,6 +279,9 @@ class SQLCompiler:
order_by = []
for field in ordering:
if hasattr(field, 'resolve_expression'):
+ if isinstance(field, Value):
+ # output_field must be resolved for constants.
+ field = Cast(field, field.output_field)
if not isinstance(field, OrderBy):
field = field.asc()
if not self.query.standard_ordering:
@@ -299,10 +303,13 @@ class SQLCompiler:
True))
continue
if col in self.query.annotations:
- # References to an expression which is masked out of the SELECT clause
- order_by.append((
- OrderBy(self.query.annotations[col], descending=descending),
- False))
+ # References to an expression which is masked out of the SELECT
+ # clause.
+ expr = self.query.annotations[col]
+ if isinstance(expr, Value):
+ # output_field must be resolved for constants.
+ expr = Cast(expr, expr.output_field)
+ order_by.append((OrderBy(expr, descending=descending), False))
continue
if '.' in field: