[Django] #36210: Support queries where lhs is CompositePrimaryKey and rhs is Subquery

15 views
Skip to first unread message

Django

unread,
Feb 23, 2025, 11:41:30 AM2/23/25
to django-...@googlegroups.com
#36210: Support queries where lhs is CompositePrimaryKey and rhs is Subquery
-------------------------------------+-------------------------------------
Reporter: Jacob | Owner: Jacob Walls
Walls |
Type: New | Status: assigned
feature |
Component: Database | Version: 5.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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)
}}}
----
{{{#!py
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:

{{{#!py
django.db.utils.DatabaseError: ORA-01796: this operator cannot be used
with lists
Help: https://docs.oracle.com/error-help/db/ora-01796/
}}}
----
[https://github.com/django/django/pull/19206 PR] to kick off discussion
addresses all but the Oracle failure, which might worth looking at
separately, perhaps a deeper issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/36210>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 23, 2025, 11:42:07 AM2/23/25
to django-...@googlegroups.com
#36210: Support queries where lhs is CompositePrimaryKey and rhs is Subquery
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36210#comment:1>

Django

unread,
Feb 24, 2025, 3:26:06 AM2/24/25
to django-...@googlegroups.com
#36210: Support queries where lhs is CompositePrimaryKey and rhs is Subquery
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* cc: Csirmaz Bendegúz (added)
* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36210#comment:2>

Django

unread,
Feb 25, 2025, 10:49:33 AM2/25/25
to django-...@googlegroups.com
#36210: Support queries where lhs is CompositePrimaryKey and rhs is Subquery
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 1 => 0

Comment:

On the PR Simon
[https://github.com/django/django/pull/19206#issuecomment-2682117828
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/36210#comment:3>

Django

unread,
May 11, 2025, 10:24:18 PM5/11/25
to django-...@googlegroups.com
#36210: Support queries where lhs is CompositePrimaryKey and rhs is Subquery
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: New feature | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/19459 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36210#comment:4>
Reply all
Reply to author
Forward
0 new messages