Opened 3 days ago

Last modified 4 hours ago

#36584 assigned Cleanup/optimization

Loosen tuple lookup check for rhs subquery field cardinality to allow selecting ForeignObject

Reported by: Jacob Walls Owned by: JaeHyuckSa
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: CompositePrimaryKey
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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:

  • tests/composite_pk/test_filter.py

    diff --git a/tests/composite_pk/test_filter.py b/tests/composite_pk/test_filter.py
    index d7ecfbec11..c4b393d6ea 100644
    a b class CompositePKFilterTests(TestCase):  
    460460        queryset = User.objects.filter(comments__in=subquery)
    461461        self.assertSequenceEqual(queryset, (self.user_2,))
    462462
     463    def test_filter_comments_by_users_subquery(self):
     464        subquery = Comment.objects.filter(id=3).values("user")
     465        queryset = Comment.objects.filter(user__in=subquery)
     466        self.assertSequenceEqual(queryset, (self.comment_3,))
     467
    463468    def test_cannot_cast_pk(self):
    464469        msg = "Cast expression does not support composite primary keys."
    465470        with self.assertRaisesMessage(ValueError, msg):

Currently this gives:

  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.

Change History (4)

comment:1 by Jacob Walls, 3 days ago

Keywords: CompositePrimaryKey added

comment:2 by JaeHyuckSa, 3 days ago

Owner: set to JaeHyuckSa
Status: newassigned

comment:3 by JaeHyuckSa, 2 days ago

Has patch: set

comment:4 by Natalia Bidart, 4 hours ago

Triage Stage: UnreviewedAccepted

Thank you Jacob for the details!

Note: See TracTickets for help on using tickets.
Back to Top