Opened 4 hours ago

Last modified 4 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: Triage Stage: Unreviewed
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 (1)

comment:1 by Jacob Walls, 4 hours ago

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