1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Fixed #36048 -- Preferred ValueError to NotSupportedError for composite pk sanity checks.

These checks are not backend-dependent.
This commit is contained in:
Jacob Walls 2025-01-02 20:41:29 -05:00 committed by Sarah Boyce
parent 51df0dff3c
commit 46b3e7dd8c
4 changed files with 9 additions and 13 deletions

View File

@ -3,7 +3,6 @@ Classes to represent the definitions of aggregate functions.
""" """
from django.core.exceptions import FieldError, FullResultSet from django.core.exceptions import FieldError, FullResultSet
from django.db import NotSupportedError
from django.db.models.expressions import Case, ColPairs, Func, Star, Value, When from django.db.models.expressions import Case, ColPairs, Func, Star, Value, When
from django.db.models.fields import IntegerField from django.db.models.fields import IntegerField
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
@ -182,7 +181,7 @@ class Count(Aggregate):
# In case of composite primary keys, count the first column. # In case of composite primary keys, count the first column.
if isinstance(expr, ColPairs): if isinstance(expr, ColPairs):
if self.distinct: if self.distinct:
raise NotSupportedError( raise ValueError(
"COUNT(DISTINCT) doesn't support composite primary keys" "COUNT(DISTINCT) doesn't support composite primary keys"
) )

View File

@ -1,4 +1,3 @@
from django.db import NotSupportedError
from django.db.models.expressions import ColPairs from django.db.models.expressions import ColPairs
from django.db.models.fields import composite from django.db.models.fields import composite
from django.db.models.fields.tuple_lookups import TupleIn, tuple_lookups from django.db.models.fields.tuple_lookups import TupleIn, tuple_lookups
@ -117,7 +116,7 @@ class RelatedLookupMixin:
def as_sql(self, compiler, connection): def as_sql(self, compiler, connection):
if isinstance(self.lhs, ColPairs): if isinstance(self.lhs, ColPairs):
if not self.rhs_is_direct_value(): if not self.rhs_is_direct_value():
raise NotSupportedError( raise ValueError(
f"'{self.lookup_name}' doesn't support multi-column subqueries." f"'{self.lookup_name}' doesn't support multi-column subqueries."
) )
self.rhs = get_normalized_value(self.rhs, self.lhs) self.rhs = get_normalized_value(self.rhs, self.lhs)

View File

@ -1,4 +1,3 @@
from django.db import NotSupportedError
from django.db.models import Count, Q from django.db.models import Count, Q
from django.test import TestCase from django.test import TestCase
@ -82,7 +81,7 @@ class CompositePKAggregateTests(TestCase):
def test_count_distinct_not_supported(self): def test_count_distinct_not_supported(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "COUNT(DISTINCT) doesn't support composite primary keys" ValueError, "COUNT(DISTINCT) doesn't support composite primary keys"
): ):
self.assertIsNone( self.assertIsNone(
User.objects.annotate(comments__count=Count("comments", distinct=True)) User.objects.annotate(comments__count=Count("comments", distinct=True))

View File

@ -1,6 +1,5 @@
import itertools import itertools
from django.db import NotSupportedError
from django.db.models import F from django.db.models import F
from django.db.models.fields.tuple_lookups import ( from django.db.models.fields.tuple_lookups import (
TupleExact, TupleExact,
@ -65,7 +64,7 @@ class TupleLookupsTests(TestCase):
def test_exact_subquery(self): def test_exact_subquery(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "'exact' doesn't support multi-column subqueries." ValueError, "'exact' doesn't support multi-column subqueries."
): ):
subquery = Customer.objects.filter(id=self.customer_1.id)[:1] subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
self.assertSequenceEqual( self.assertSequenceEqual(
@ -239,7 +238,7 @@ class TupleLookupsTests(TestCase):
def test_lt_subquery(self): def test_lt_subquery(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "'lt' doesn't support multi-column subqueries." ValueError, "'lt' doesn't support multi-column subqueries."
): ):
subquery = Customer.objects.filter(id=self.customer_1.id)[:1] subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
self.assertSequenceEqual( self.assertSequenceEqual(
@ -287,7 +286,7 @@ class TupleLookupsTests(TestCase):
def test_lte_subquery(self): def test_lte_subquery(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "'lte' doesn't support multi-column subqueries." ValueError, "'lte' doesn't support multi-column subqueries."
): ):
subquery = Customer.objects.filter(id=self.customer_1.id)[:1] subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
self.assertSequenceEqual( self.assertSequenceEqual(
@ -327,7 +326,7 @@ class TupleLookupsTests(TestCase):
def test_gt_subquery(self): def test_gt_subquery(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "'gt' doesn't support multi-column subqueries." ValueError, "'gt' doesn't support multi-column subqueries."
): ):
subquery = Customer.objects.filter(id=self.customer_1.id)[:1] subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
self.assertSequenceEqual( self.assertSequenceEqual(
@ -375,7 +374,7 @@ class TupleLookupsTests(TestCase):
def test_gte_subquery(self): def test_gte_subquery(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "'gte' doesn't support multi-column subqueries." ValueError, "'gte' doesn't support multi-column subqueries."
): ):
subquery = Customer.objects.filter(id=self.customer_1.id)[:1] subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
self.assertSequenceEqual( self.assertSequenceEqual(
@ -419,7 +418,7 @@ class TupleLookupsTests(TestCase):
def test_isnull_subquery(self): def test_isnull_subquery(self):
with self.assertRaisesMessage( with self.assertRaisesMessage(
NotSupportedError, "'isnull' doesn't support multi-column subqueries." ValueError, "'isnull' doesn't support multi-column subqueries."
): ):
subquery = Customer.objects.filter(id=0)[:1] subquery = Customer.objects.filter(id=0)[:1]
self.assertSequenceEqual( self.assertSequenceEqual(