#36065: order_by("pk") not equivalent to order_by(F("pk")) for CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: dev | Severity: Release
| blocker
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
For CompositePrimaryKey, `order_by("pk")` and `order_by(F("pk"))` produce
different results.
For `"pk"`, direction is distributed to all cols `(col1 ASC, col2 ASC)`,
but for `F("pk")`, it is not: `(col1, col2 ASC)`.
Failing test:
{{{#!diff
diff --git a/tests/composite_pk/test_filter.py
b/tests/composite_pk/test_filter.py
index 7e361c5925..81bbfc65be 100644
--- a/tests/composite_pk/test_filter.py
+++ b/tests/composite_pk/test_filter.py
@@ -1,3 +1,4 @@
+from django.db.models import F
from django.test import TestCase
from .models import Comment, Tenant, User
@@ -78,6 +79,12 @@ class CompositePKFilterTests(TestCase):
),
)
+ def test_order_by_comments_by_pk_asc_f(self):
+ self.assertSequenceEqual(
+ Comment.objects.order_by("pk"),
+ Comment.objects.order_by(F("pk")),
+ )
+
def test_filter_comments_by_pk_gt(self):
c11, c12, c13, c24, c15 = (
self.comment_1,
}}}
{{{
======================================================================
FAIL: test_order_by_comments_by_pk_asc_f
(composite_pk.test_filter.CompositePKFilterTests.test_order_by_comments_by_pk_asc_f)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/user/django/tests/composite_pk/test_filter.py", line 83, in
test_order_by_comments_by_pk_asc_f
self.assertSequenceEqual(
AssertionError: Sequences differ: <Quer[123 chars] Comment object ((1,
5))>, <Comment: Comment object ((2, 4))>]> != <Quer[123 chars] Comment
object ((1, 5))>, <Comment: Comment object ((2, 4))>]>
<QuerySet [<Comment: Comment object ((1, 1))>, <Comment: Comment object
((1, 2))>, <Comment: Comment object ((1, 3))>, <Comment: Comment object
((1, 5))>, <Comment: Comment object ((2, 4))>]>
----------------------------------------------------------------------
(0.000)
SELECT "composite_pk_comment"."tenant_id",
"composite_pk_comment"."comment_id",
"composite_pk_comment"."user_id",
"composite_pk_comment"."text"
FROM "composite_pk_comment"
ORDER BY "composite_pk_comment"."tenant_id" ASC,
"composite_pk_comment"."comment_id" ASC;
args=();
ALIAS=DEFAULT (0.000)
SELECT "composite_pk_comment"."tenant_id",
"composite_pk_comment"."comment_id",
"composite_pk_comment"."user_id",
"composite_pk_comment"."text"
FROM "composite_pk_comment"
ORDER BY "composite_pk_comment"."tenant_id",
"composite_pk_comment"."comment_id" ASC;
args=();
ALIAS=DEFAULT
----------------------------------------------------------------------
Ran 103 tests in 0.107s
FAILED (failures=1)
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36065>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.