mirror of
https://github.com/django/django.git
synced 2025-02-05 23:15:29 +00:00
Fixed #36065 -- Fixed ordering by expression referencing composite primary key.
Thanks Jacob Walls for the report and test and Csirmaz Bendegúz for the review.
This commit is contained in:
parent
7617d5be94
commit
42e8f264ce
@ -1861,6 +1861,16 @@ class OrderBy(Expression):
|
||||
return [self.expression]
|
||||
|
||||
def as_sql(self, compiler, connection, template=None, **extra_context):
|
||||
if isinstance(self.expression, ColPairs):
|
||||
sql_parts = []
|
||||
params = []
|
||||
for col in self.expression.get_cols():
|
||||
copy = self.copy()
|
||||
copy.set_source_expressions([col])
|
||||
sql, col_params = compiler.compile(copy)
|
||||
sql_parts.append(sql)
|
||||
params.extend(col_params)
|
||||
return ", ".join(sql_parts), params
|
||||
template = template or self.template
|
||||
if connection.features.supports_order_by_nulls_modifier:
|
||||
if self.nulls_last:
|
||||
|
@ -1117,10 +1117,9 @@ class SQLCompiler:
|
||||
)
|
||||
return results
|
||||
targets, alias, _ = self.query.trim_joins(targets, joins, path)
|
||||
target_fields = composite.unnest(targets)
|
||||
return [
|
||||
(OrderBy(transform_function(t, alias), descending=descending), False)
|
||||
for t in target_fields
|
||||
for t in targets
|
||||
]
|
||||
|
||||
def _setup_joins(self, pieces, opts, alias):
|
||||
|
@ -1,3 +1,4 @@
|
||||
from django.db.models import F
|
||||
from django.test import TestCase
|
||||
|
||||
from .models import Comment, Tenant, User
|
||||
@ -55,3 +56,17 @@ class CompositePKOrderByTests(TestCase):
|
||||
self.comment_1, # (1, 1)
|
||||
),
|
||||
)
|
||||
|
||||
def test_order_comments_by_pk_expr(self):
|
||||
self.assertQuerySetEqual(
|
||||
Comment.objects.order_by("pk"),
|
||||
Comment.objects.order_by(F("pk")),
|
||||
)
|
||||
self.assertQuerySetEqual(
|
||||
Comment.objects.order_by("-pk"),
|
||||
Comment.objects.order_by(F("pk").desc()),
|
||||
)
|
||||
self.assertQuerySetEqual(
|
||||
Comment.objects.order_by("-pk"),
|
||||
Comment.objects.order_by(F("pk").desc(nulls_last=True)),
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user