Opened 4 months ago
Last modified 7 weeks ago
#36210 assigned New feature
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: | Accepted |
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 (7)
comment:1 by , 4 months ago
Has patch: | set |
---|
comment:2 by , 4 months ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 4 months ago
Has patch: | unset |
---|
comment:5 by , 8 weeks ago
Summary: | Support queries where lhs is CompositePrimaryKey and rhs is Subquery → Support exact lookups where lhs is CompositePrimaryKey and rhs is Subquery |
---|
comment:6 by , 8 weeks 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 , 7 weeks ago
Patch needs improvement: | 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.