8000 Fixed #36181 -- Allowed Subquery usage in __in lookups against compos… · django/django@8561100 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8561100

Browse files
charettessarahboyce
authored andcommitted
Fixed #36181 -- Allowed Subquery usage in __in lookups against composite pks.
Thanks Jacob Walls for the report.
1 parent 47c837a commit 8561100

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

django/db/models/fields/tuple_lookups.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
from django.core.exceptions import EmptyResultSet
44
from django.db.models import Field
5-
from django.db.models.expressions import ColPairs, Func, ResolvedOuterRef, Value
5+
from django.db.models.expressions import (
6+
ColPairs,
7+
Func,
8+
ResolvedOuterRef,
9+
Subquery,
10+
Value,
11+
)
612
from django.db.models.lookups import (
713
Exact,
814
GreaterThan,
@@ -301,7 +307,7 @@ def check_rhs_elements_length_equals_lhs_length(self):
301307
)
302308

303309
def check_rhs_is_query(self):
304-
if not isinstance(self.rhs, Query):
310+
if not isinstance(self.rhs, (Query, Subquery)):
305311
lhs_str = self.get_lhs_str()
306312
rhs_cls = self.rhs.__class__.__name__
307313
raise ValueError(

tests/composite_pk/test_filter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ def test_cannot_cast_pk(self):
442442
with self.assertRaisesMessage(ValueError, msg):
443443
Comment.objects.filter(text__gt=Cast(F("pk"), TextField())).count()
444444

445+
def test_explicit_subquery(self):
446+
subquery = Subquery(User.objects.values("pk"))
447+
self.assertEqual(User.objects.filter(pk__in=subquery).count(), 4)
448+
self.assertEqual(Comment.objects.filter(user__in=subquery).count(), 5)
449+
445450
def test_filter_case_when(self):
446451
msg = "When expression does not support composite primary keys."
447452
with self.assertRaisesMessage(ValueError, msg):

0 commit comments

Comments
 (0)
0