#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.