Opened 6 months ago

Closed 4 weeks ago

Last modified 3 weeks ago

#36210 closed New feature (fixed)

Support exact lookups where lhs is CompositePrimaryKey and rhs is Subquery

Reported by: Jacob Walls Owned by: Jacob Walls
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords:
Cc: Csirmaz Bendegúz Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This query is not currently supported (models drawn from composite_pk test suite):

subquery = Subquery(User.objects.filter(pk=OuterRef("pk")).values("pk"))
queryset = Comment.objects.filter(pk=subquery)

  File "/django/source/django/db/models/fields/tuple_lookups.py", line 56, in get_prep_lookup
    self.check_rhs_is_supported_expression()
  File "/django/source/django/db/models/fields/tuple_lookups.py", line 79, in check_rhs_is_supported_expression
    raise ValueError(
ValueError: 'exact' subquery lookup of 'pk' only supports OuterRef and QuerySet objects (received 'Subquery')

The version without explicit Subquery (and adding [:1]) also fails:

  File "/django/source/django/db/models/lookups.py", line 384, in get_prep_lookup
    raise ValueError(
ValueError: The QuerySet value for the exact lookup must have 2 selected fields (received 1)

On Oracle the failure looks like this, but this is probably a separate issue:

django.db.utils.DatabaseError: ORA-01796: this operator cannot be used with lists
Help: https://docs.oracle.com/error-help/db/ora-01796/

PR to kick off discussion addresses all but the Oracle failure, which might worth looking at separately, perhaps a deeper issue?

Change History (14)

comment:1 by Jacob Walls, 6 months ago

Has patch: set

comment:2 by Sarah Boyce, 6 months ago

Cc: Csirmaz Bendegúz added
Triage Stage: UnreviewedAccepted

comment:3 by Jacob Walls, 6 months ago

Has patch: unset

On the PR Simon suggested fixing this closer to the root of the problems with Subquery resolution. If someone else has capacity, feel free to reassign, it might be a while before I can get to it.

comment:4 by Jacob Walls, 4 months ago

Has patch: set

comment:5 by Jacob Walls, 4 months ago

Summary: Support queries where lhs is CompositePrimaryKey and rhs is SubquerySupport exact lookups where lhs is CompositePrimaryKey and rhs is Subquery

comment:6 by Simon Charette, 4 months ago

Patch needs improvement: set

Left some comments about the preferred usage of a feature flag for tuple comparison against subquery support but otherwise the patch looks great.

comment:7 by Jacob Walls, 4 months ago

Patch needs improvement: unset

comment:8 by Sarah Boyce, 4 weeks ago

Patch needs improvement: set

comment:9 by Jacob Walls, 4 weeks ago

Patch needs improvement: unset

comment:10 by Sarah Boyce, 4 weeks ago

Triage Stage: AcceptedReady for checkin

comment:11 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

In de7bb7e:

Refs #36210 -- Added missing limits in Subquery tests.

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In fd569dd:

Fixed #36210, Refs #36181 -- Allowed Subquery usage in further lookups against composite pks.

Follow-up to 8561100425876bde3be4b2a22324655f74ff9609.

Co-authored-by: Simon Charette <charette.s@…>

comment:13 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

In 181e3bd7:

Refs #36210 -- Corrected output_field comparison in Subquery.resolve_expression().

Regression in fd569dd45bf0746378faf7f65172497f21ed27f0.

comment:14 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

In eed7f442:

Refs #36210 -- Added release note for further subquery support for composite pks.

Follow-up to fd569dd45bf0746378faf7f65172497f21ed27f0.

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