Opened 63 minutes ago

Last modified 55 minutes ago

#37383 assigned Bug

Add support for using F("pk") as the LHS of a tuple lookup when the RHS is a Query

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

Description

The fix for #37291 fixes a hang when using F("pk") with tuple lookups.

The preliminary fix results in ValueError, which is better than a hang. That much can be backported. This issue is for improving on the ValueError to return the correct result. In other words, this ticket is for removing these skips on these almost-identical tests:

# For TupleLookupsTests
    @unittest.expectedFailure
    def test_tuple_in_subquery_lhs_f_pk(self):
        self.assertCountEqual(
            Contact.objects.filter(
                TupleIn(F("pk"), Contact.objects.values("customer").query)
            ),
            Contact.objects.all(),
        )
# For CompositePKLookupsTests
    @unittest.expectedFailure
    def test_filter_by_pk_in_rhs_f_object_lhs(self):
        self.assertCountEqual(
            Comment.objects.filter(
                TupleIn(F("pk"), Comment.objects.values("pk").query)
            ),
            Comment.objects.all(),
        )

See

======================================================================
ERROR: test_tuple_in_subquery_lhs_f_pk (foreign_object.test_tuple_lookups.TupleLookupsTests.test_tuple_in_subquery_lhs_f_pk)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jwalls/django/tests/foreign_object/test_tuple_lookups.py", line 174, in test_tuple_in_subquery_lhs_f_pk
    TupleIn(F("pk"), Contact.objects.values("customer").query)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jwalls/django/django/db/models/lookups.py", line 36, in __init__
    self.rhs = self.get_prep_lookup()
               ~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/jwalls/django/django/db/models/fields/tuple_lookups.py", line 310, in get_prep_lookup
    super(TupleLookupMixin, self).get_prep_lookup()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/jwalls/django/django/db/models/lookups.py", line 512, in get_prep_lookup
    raise ValueError(
    ...<2 lines>...
    )
ValueError: The QuerySet value for the 'in' lookup must have 1 selected fields (received 2)

We don't have access to the query for resolution of F("pk") during the get_prep_lookup() above, so I'm not immediately sure how to solve this.

Change History (1)

comment:1 by Vishy, 55 minutes ago

Owner: set to Vishy
Status: new → assigned
Note: See TracTickets for help on using tickets.
Back to Top