1
0
mirror of https://github.com/django/django.git synced 2025-06-08 13:09:13 +00:00

Fixed #36392 -- Raised ValueError when subquery referencing composite pk selects too many columns.

This commit is contained in:
Jacob Walls 2025-05-14 22:49:52 -04:00 committed by Sarah Boyce
parent e03e5c751c
commit 994dc6d8a1
3 changed files with 16 additions and 1 deletions

View File

@ -1227,7 +1227,10 @@ class Query(BaseExpression):
@property
def _subquery_fields_len(self):
if self.has_select_fields:
return len(self.selected)
return sum(
len(self.model._meta.pk_fields) if field == "pk" else 1
for field in self.selected
)
return len(self.model._meta.pk_fields)
def resolve_expression(self, query, *args, **kwargs):

View File

@ -11,3 +11,7 @@ Bugfixes
* Fixed a crash when using ``select_related`` against a ``ForeignObject``
originating from a model with a ``CompositePrimaryKey`` (:ticket:`36373`).
* Fixed a bug in Django 5.2 where subqueries using ``"pk"`` to reference models
with a ``CompositePrimaryKey`` failed to raise ``ValueError`` when too many
or too few columns were selected (:ticket:`36392`).

View File

@ -206,6 +206,14 @@ class CompositePKFilterTests(TestCase):
[self.comment_1],
)
def test_filter_by_pk_in_subquery_invalid_selected_columns(self):
msg = (
"The QuerySet value for the 'in' lookup must have 2 selected "
"fields (received 3)"
)
with self.assertRaisesMessage(ValueError, msg):
Comment.objects.filter(pk__in=Comment.objects.values("pk", "text"))
def test_filter_by_pk_in_none(self):
with self.assertNumQueries(0):
self.assertSequenceEqual(