[Django] #36050: Cannot resolve OuterRef to CompositePrimaryKey

15 views
Skip to first unread message

Django

unread,
Jan 1, 2025, 1:27:43 PMJan 1
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type: New
| feature
Status: new | Component: Database
| layer (models, ORM)
Version: dev | Severity: Normal
Keywords: subquery | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
With CompositePrimaryKey, using `OuterRef("pk")` in a subquery raises
ValueError. Not certain if this is a release blocker or a new feature. If
it's a new feature, we might want to document that it's not currently
supported.

{{{#!diff
diff --git a/tests/composite_pk/test_filter.py
b/tests/composite_pk/test_filter.py
index 7e361c5925..c3dfc8d44b 100644
--- a/tests/composite_pk/test_filter.py
+++ b/tests/composite_pk/test_filter.py
@@ -1,3 +1,4 @@
+from django.db.models import OuterRef, Subquery
from django.test import TestCase

from .models import Comment, Tenant, User
@@ -54,6 +55,12 @@ class CompositePKFilterTests(TestCase):
with self.subTest(lookup=lookup, count=count):
self.assertEqual(User.objects.filter(**lookup).count(),
count)

+ def test_outer_ref_pk(self):
+ qs = Comment.objects.filter(
+
id__gt=Subquery(Comment.objects.filter(pk=OuterRef("pk")).values("id")[:1])
+ )
+ self.assertEqual(qs.count(), 0)
+
def test_order_comments_by_pk_asc(self):
self.assertSequenceEqual(
Comment.objects.order_by("pk"),
}}}
{{{
File "/Users/.../django/django/db/models/fields/tuple_lookups.py", line
32, in get_prep_lookup
self.check_rhs_is_tuple_or_list()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/Users/.../django/django/db/models/fields/tuple_lookups.py", line
39, in check_rhs_is_tuple_or_list
raise ValueError(
f"{self.lookup_name!r} lookup of {lhs_str} must be a tuple or a
list"
)
ValueError: 'exact' lookup of 'pk' must be a tuple or a list
}}}
----
Adjusting the guard that raises ValueError to also accept
`ResolvedOuterRef` leads to a more interesting failure:
{{{
File "/Users/.../django/django/db/models/expressions.py", line 1162, in
as_sql
val = output_field.get_db_prep_value(val, connection=connection)
File "/Users/.../django/django/db/models/fields/related.py", line 1161,
in get_db_prep_value
return self.target_field.get_db_prep_value(value, connection,
prepared)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/.../django/django/db/models/fields/__init__.py", line 2819,
in get_db_prep_value
value = self.get_prep_value(value)
File "/Users/.../django/django/db/models/fields/__init__.py", line 2129,
in get_prep_value
raise e.__class__(
"Field '%s' expected a number but got %r." % (self.name, value),
) from e
TypeError: Field 'id' expected a number but got Col(composite_pk_comment,
composite_pk.Comment.tenant).
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36050>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 2, 2025, 3:41:27 AMJan 2
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 0 | 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)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
* type: New feature => Bug

Comment:

Thank you Jacob
Marking as a release blocker. I think we might be able to resolve this but
if not, agree we might want to document it within the not supported
section.
--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:1>

Django

unread,
Jan 2, 2025, 4:15:27 AMJan 2
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):

Note that the "document" fix can include a better error message.
--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:2>

Django

unread,
Jan 3, 2025, 4:06:21 AMJan 3
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Csirmaz Bendegúz):

* owner: (none) => Csirmaz Bendegúz
* status: new => assigned

Comment:

Thank you, I'll look into it
--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:3>

Django

unread,
Jan 3, 2025, 6:13:16 AMJan 3
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Csirmaz Bendegúz):

* has_patch: 0 => 1

Comment:

I added a patch
--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:4>

Django

unread,
Jan 3, 2025, 10:27:40 AMJan 3
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:5>

Django

unread,
Jan 7, 2025, 5:41:58 AMJan 7
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | 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):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:6>

Django

unread,
Jan 8, 2025, 9:23:36 AMJan 8
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:7>

Django

unread,
Jan 10, 2025, 1:31:39 AMJan 10
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Csirmaz Bendegúz):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:8>

Django

unread,
Jan 10, 2025, 5:51:54 AMJan 10
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: subquery | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:9>

Django

unread,
Jan 10, 2025, 8:38:18 AMJan 10
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: subquery | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"8bee7fa45cd7bfe70b68784314e994e2d193fd70" 8bee7fa4]:
{{{#!CommitTicketReference repository=""
revision="8bee7fa45cd7bfe70b68784314e994e2d193fd70"
Fixed #36050 -- Added OuterRef support to CompositePrimaryKey.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:10>

Django

unread,
Jan 13, 2025, 5:15:01 AMJan 13
to django-...@googlegroups.com
#36050: Cannot resolve OuterRef to CompositePrimaryKey
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Csirmaz
| Bendegúz
Type: Bug | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: subquery | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"76a0b6aa3aa7bd62c2a9ba11306c906047ee7ada" 76a0b6aa]:
{{{#!CommitTicketReference repository=""
revision="76a0b6aa3aa7bd62c2a9ba11306c906047ee7ada"
Refs #36050 -- Fixed OuterRef support for CompositePrimaryKey on Oracle.

Oracle doesn't support native tuple comparison so each as_oracle
implementation
of tuple lookups must also perform right-hand-side sanitization.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36050#comment:11>
Reply all
Reply to author
Forward
0 new messages