Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Django] #36392: ValueError not raised when wrong number of columns selected by subquery against composite primary key

10 views
Skip to first unread message

Django

unread,
May 14, 2025, 10:53:57 AMMay 14
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.2 | Severity: Release
| blocker
Keywords: pk | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Discovered during
[https://github.com/django/django/pull/19459#discussion_r2087862732
review] of #36210 that the guard for the appropriate number of columns
selected in a subquery is not aware that `"pk"` might represent N columns,
so the `ValueError` that should be raised is evaded, failing instead at
the db layer.

We're just changing one type of exception to another--so this isn't
terribly serious--but in the interest of standardizing what kinds of
exceptions are raised during usage of the composite pk feature, starting
out at release blocker?

{{{#!diff
diff --git a/tests/composite_pk/test_filter.py
b/tests/composite_pk/test_filter.py
index 16c4c2a91a..f629f85ca6 100644
--- a/tests/composite_pk/test_filter.py
+++ b/tests/composite_pk/test_filter.py
@@ -207,6 +207,10 @@ class CompositePKFilterTests(TestCase):
[self.comment_1],
)

+ def test_invalid_filter_by_pk_in_columns_besides_pk(self):
+ with self.assertRaisesMessage(ValueError, "must have 2 selected
fields"):
+ User.objects.filter(pk__in=User.objects.values("pk",
"email")).count()
+
def test_filter_by_pk_in_none(self):
with self.assertNumQueries(0):
self.assertSequenceEqual(
}}}

----
Postgres failure looks like:
{{{#!py
File "/Users/.../django/django/db/backends/sqlite3/base.py", line 360,
in execute
return super().execute(query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: sub-select returns 3 columns - expected
2

}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36392>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 14, 2025, 10:59:03 AMMay 14
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: pk | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* stage: Unreviewed => Accepted

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

Django

unread,
May 14, 2025, 10:31:15 PMMay 14
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: pk | 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):

* owner: (none) => Jacob Walls
* status: new => assigned

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

Django

unread,
May 14, 2025, 11:01:39 PMMay 14
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: pk | 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/19469 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36392#comment:3>

Django

unread,
May 15, 2025, 4:24:45 AMMay 15
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: pk | 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/36392#comment:4>

Django

unread,
May 15, 2025, 8:35:10 AMMay 15
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: pk | 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/36392#comment:5>

Django

unread,
May 15, 2025, 8:45:17 AMMay 15
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: assigned
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: pk | 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/36392#comment:6>

Django

unread,
May 16, 2025, 2:20:10 AMMay 16
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: pk | 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:"994dc6d8a1bae717baa236b65e11cf91ce181c53" 994dc6d8]:
{{{#!CommitTicketReference repository=""
revision="994dc6d8a1bae717baa236b65e11cf91ce181c53"
Fixed #36392 -- Raised ValueError when subquery referencing composite pk
selects too many columns.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36392#comment:7>

Django

unread,
May 16, 2025, 2:21:57 AMMay 16
to django-...@googlegroups.com
#36392: ValueError not raised when wrong number of columns selected by subquery
against composite primary key
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: Jacob
| Walls
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: pk | 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:"6228a35095d06fecf55bc1a3308ab4d46cc2d57b" 6228a35]:
{{{#!CommitTicketReference repository=""
revision="6228a35095d06fecf55bc1a3308ab4d46cc2d57b"
[5.2.x] Fixed #36392 -- Raised ValueError when subquery referencing
composite pk selects too many columns.

Backport of 994dc6d8a1bae717baa236b65e11cf91ce181c53 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36392#comment:8>
Reply all
Reply to author
Forward
0 new messages