mirror of
https://github.com/django/django.git
synced 2025-06-17 09:29:17 +00:00
[5.2.x] Fixed #36392 -- Raised ValueError when subquery referencing composite pk selects too many columns.
Backport of 994dc6d8a1bae717baa236b65e11cf91ce181c53 from main.
This commit is contained in:
parent
954e24758c
commit
6228a35095
@ -1227,7 +1227,10 @@ class Query(BaseExpression):
|
|||||||
@property
|
@property
|
||||||
def _subquery_fields_len(self):
|
def _subquery_fields_len(self):
|
||||||
if self.has_select_fields:
|
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)
|
return len(self.model._meta.pk_fields)
|
||||||
|
|
||||||
def resolve_expression(self, query, *args, **kwargs):
|
def resolve_expression(self, query, *args, **kwargs):
|
||||||
|
@ -11,3 +11,7 @@ Bugfixes
|
|||||||
|
|
||||||
* Fixed a crash when using ``select_related`` against a ``ForeignObject``
|
* Fixed a crash when using ``select_related`` against a ``ForeignObject``
|
||||||
originating from a model with a ``CompositePrimaryKey`` (:ticket:`36373`).
|
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],
|
[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):
|
def test_filter_by_pk_in_none(self):
|
||||||
with self.assertNumQueries(0):
|
with self.assertNumQueries(0):
|
||||||
self.assertSequenceEqual(
|
self.assertSequenceEqual(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user