Opened 3 days ago

Last modified 15 hours ago

#36210 assigned New feature

Support queries 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: Accepted
Has patch: no 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 (3)

comment:1 by Jacob Walls, 3 days ago

Has patch: set

comment:2 by Sarah Boyce, 2 days ago

Cc: Csirmaz Bendegúz added
Triage Stage: UnreviewedAccepted

comment:3 by Jacob Walls, 15 hours 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.

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