1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #36042 -- Raised ValueError when using CompositePrimaryKey as rhs.

This commit is contained in:
Jacob Walls
2024-12-31 13:10:35 -05:00
committed by Sarah Boyce
parent 8914b571eb
commit 6eec703667
3 changed files with 28 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
from django.db.models import F, TextField
from django.db.models.functions import Cast
from django.test import TestCase
from .models import Comment, Tenant, User
@@ -54,6 +56,20 @@ class CompositePKFilterTests(TestCase):
with self.subTest(lookup=lookup, count=count):
self.assertEqual(User.objects.filter(**lookup).count(), count)
def test_rhs_pk(self):
msg = "CompositePrimaryKey cannot be used as a lookup value."
with self.assertRaisesMessage(ValueError, msg):
Comment.objects.filter(text__gt=F("pk")).count()
def test_rhs_combinable(self):
msg = "CompositePrimaryKey is not combinable."
for expr in [F("pk") + (1, 1), (1, 1) + F("pk")]:
with (
self.subTest(expression=expr),
self.assertRaisesMessage(ValueError, msg),
):
Comment.objects.filter(text__gt=expr).count()
def test_order_comments_by_pk_asc(self):
self.assertSequenceEqual(
Comment.objects.order_by("pk"),
@@ -410,3 +426,8 @@ class CompositePKFilterTests(TestCase):
subquery = Comment.objects.filter(id=3).only("pk")
queryset = User.objects.filter(comments__in=subquery)
self.assertSequenceEqual(queryset, (self.user_2,))
def test_cannot_cast_pk(self):
msg = "Casting CompositePrimaryKey is not supported."
with self.assertRaisesMessage(ValueError, msg):
Comment.objects.filter(text__gt=Cast(F("pk"), TextField())).count()