[Django] #36584: Loosen tuple lookup check for rhs subquery field cardinality to allow selecting ForeignObject

5 views
Skip to first unread message

Django

unread,
Aug 29, 2025, 11:05:28 AM (7 days ago) Aug 29
to django-...@googlegroups.com
#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.

Django

unread,
Aug 29, 2025, 11:06:38 AM (7 days ago) Aug 29
to django-...@googlegroups.com
#36584: Loosen tuple lookup check for rhs subquery field cardinality to allow
selecting ForeignObject
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: CompositePrimaryKey | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* keywords: => CompositePrimaryKey

--
Ticket URL: <https://code.djangoproject.com/ticket/36584#comment:1>

Django

unread,
Aug 29, 2025, 1:20:52 PM (7 days ago) Aug 29
to django-...@googlegroups.com
#36584: Loosen tuple lookup check for rhs subquery field cardinality to allow
selecting ForeignObject
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
Type: | JaeHyuckSa
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: CompositePrimaryKey | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by JaeHyuckSa):

* owner: (none) => JaeHyuckSa
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36584#comment:2>

Django

unread,
Aug 30, 2025, 9:24:57 AM (6 days ago) Aug 30
to django-...@googlegroups.com
#36584: Loosen tuple lookup check for rhs subquery field cardinality to allow
selecting ForeignObject
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
Type: | JaeHyuckSa
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: CompositePrimaryKey | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by JaeHyuckSa):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36584#comment:3>

Django

unread,
Sep 1, 2025, 12:07:20 PM (4 days ago) Sep 1
to django-...@googlegroups.com
#36584: Loosen tuple lookup check for rhs subquery field cardinality to allow
selecting ForeignObject
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner:
Type: | JaeHyuckSa
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: CompositePrimaryKey | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* stage: Unreviewed => Accepted

Comment:

Thank you Jacob for the details!
--
Ticket URL: <https://code.djangoproject.com/ticket/36584#comment:4>
Reply all
Reply to author
Forward
0 new messages