#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070198f65c9e2e-55354a34-195c-4c12-8ff8-4338a77b8a82-000000%40eu-central-1.amazonses.com.

Reply via email to