1
0
mirror of https://github.com/django/django.git synced 2025-03-06 07:22:32 +00:00

[5.2.x] Fixed #36181 -- Allowed Subquery usage in __in lookups against composite pks.

Thanks Jacob Walls for the report.

Backport of 8561100425876bde3be4b2a22324655f74ff9609 from main.
This commit is contained in:
Simon Charette 2025-02-09 21:42:06 -05:00 committed by Sarah Boyce
parent dc1c9b4ddd
commit 771c250b10
2 changed files with 13 additions and 2 deletions

View File

@ -2,7 +2,13 @@ import itertools
from django.core.exceptions import EmptyResultSet
from django.db.models import Field
from django.db.models.expressions import ColPairs, Func, ResolvedOuterRef, Value
from django.db.models.expressions import (
ColPairs,
Func,
ResolvedOuterRef,
Subquery,
Value,
)
from django.db.models.lookups import (
Exact,
GreaterThan,
@ -290,7 +296,7 @@ class TupleIn(TupleLookupMixin, In):
)
def check_rhs_is_query(self):
if not isinstance(self.rhs, Query):
if not isinstance(self.rhs, (Query, Subquery)):
lhs_str = self.get_lhs_str()
rhs_cls = self.rhs.__class__.__name__
raise ValueError(

View File

@ -442,6 +442,11 @@ class CompositePKFilterTests(TestCase):
with self.assertRaisesMessage(ValueError, msg):
Comment.objects.filter(text__gt=Cast(F("pk"), TextField())).count()
def test_explicit_subquery(self):
subquery = Subquery(User.objects.values("pk"))
self.assertEqual(User.objects.filter(pk__in=subquery).count(), 4)
self.assertEqual(Comment.objects.filter(user__in=subquery).count(), 5)
def test_filter_case_when(self):
msg = "When expression does not support composite primary keys."
with self.assertRaisesMessage(ValueError, msg):