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:
parent
e03e5c751c
commit
994dc6d8a1
@ -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):
|
||||
|
@ -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`).
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user