#36584: Loosen tuple lookup check for rhs subquery field cardinality to allow
selecting ForeignObject
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type:
| Cleanup/optimization
Status: new | Component: Database
| layer (models, ORM)
Version: dev | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
As of the fix for #36431, `values("user")` where "user" is a multi-column
`ForeignObject` now resolves to a `Tuple`, which means we can relax the
cardinality check for lookups such as `TupleIn` (introduced in #36149) to
get this test case passing:
{{{#!diff
diff --git a/tests/composite_pk/test_filter.py
b/tests/composite_pk/test_filter.py
index d7ecfbec11..c4b393d6ea 100644
--- a/tests/composite_pk/test_filter.py
+++ b/tests/composite_pk/test_filter.py
@@ -460,6 +460,11 @@ class CompositePKFilterTests(TestCase):
queryset = User.objects.filter(comments__in=subquery)
self.assertSequenceEqual(queryset, (self.user_2,))
+ def test_filter_comments_by_users_subquery(self):
+ subquery = Comment.objects.filter(id=3).values("user")
+ queryset = Comment.objects.filter(user__in=subquery)
+ self.assertSequenceEqual(queryset, (self.comment_3,))
+
def test_cannot_cast_pk(self):
msg = "Cast expression does not support composite primary keys."
with self.assertRaisesMessage(ValueError, msg):
}}}
Currently this gives:
{{{#!py
File "/Users/jwalls/django/django/db/models/fields/related_lookups.py",
line 84, in get_prep_lookup
return super().get_prep_lookup()
~~~~~~~~~~~~~~~~~~~~~~~^^
File "/Users/jwalls/django/django/db/models/lookups.py", line 509, in
get_prep_lookup
raise ValueError(
...<2 lines>...
)
ValueError: The QuerySet value for the 'in' lookup must have 2 selected
fields (received 1)
----------------------------------------------------------------------
}}}
Indeed, commenting out that `ValueError` allows this test to pass with the
fix for #36431, so there should be way to adjust this guard.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36584>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.