From 46b3e7dd8cc1792a40bb8b4d0c267f3d1ceef68c Mon Sep 17 00:00:00 2001
From: Jacob Walls <jacobtylerwalls@gmail.com>
Date: Thu, 2 Jan 2025 20:41:29 -0500
Subject: [PATCH] Fixed #36048 -- Preferred ValueError to NotSupportedError for
 composite pk sanity checks.

These checks are not backend-dependent.
---
 django/db/models/aggregates.py             |  3 +--
 django/db/models/fields/related_lookups.py |  3 +--
 tests/composite_pk/test_aggregate.py       |  3 +--
 tests/foreign_object/test_tuple_lookups.py | 13 ++++++-------
 4 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/django/db/models/aggregates.py b/django/db/models/aggregates.py
index 98d173b958..8491506aad 100644
--- a/django/db/models/aggregates.py
+++ b/django/db/models/aggregates.py
@@ -3,7 +3,6 @@ Classes to represent the definitions of aggregate functions.
 """
 
 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.fields import IntegerField
 from django.db.models.functions import Coalesce
@@ -182,7 +181,7 @@ class Count(Aggregate):
         # In case of composite primary keys, count the first column.
         if isinstance(expr, ColPairs):
             if self.distinct:
-                raise NotSupportedError(
+                raise ValueError(
                     "COUNT(DISTINCT) doesn't support composite primary keys"
                 )
 
diff --git a/django/db/models/fields/related_lookups.py b/django/db/models/fields/related_lookups.py
index 6992d75833..a6e28b11fb 100644
--- a/django/db/models/fields/related_lookups.py
+++ b/django/db/models/fields/related_lookups.py
@@ -1,4 +1,3 @@
-from django.db import NotSupportedError
 from django.db.models.expressions import ColPairs
 from django.db.models.fields import composite
 from django.db.models.fields.tuple_lookups import TupleIn, tuple_lookups
@@ -117,7 +116,7 @@ class RelatedLookupMixin:
     def as_sql(self, compiler, connection):
         if isinstance(self.lhs, ColPairs):
             if not self.rhs_is_direct_value():
-                raise NotSupportedError(
+                raise ValueError(
                     f"'{self.lookup_name}' doesn't support multi-column subqueries."
                 )
             self.rhs = get_normalized_value(self.rhs, self.lhs)
diff --git a/tests/composite_pk/test_aggregate.py b/tests/composite_pk/test_aggregate.py
index b5474c5218..7b40bffe9a 100644
--- a/tests/composite_pk/test_aggregate.py
+++ b/tests/composite_pk/test_aggregate.py
@@ -1,4 +1,3 @@
-from django.db import NotSupportedError
 from django.db.models import Count, Q
 from django.test import TestCase
 
@@ -82,7 +81,7 @@ class CompositePKAggregateTests(TestCase):
 
     def test_count_distinct_not_supported(self):
         with self.assertRaisesMessage(
-            NotSupportedError, "COUNT(DISTINCT) doesn't support composite primary keys"
+            ValueError, "COUNT(DISTINCT) doesn't support composite primary keys"
         ):
             self.assertIsNone(
                 User.objects.annotate(comments__count=Count("comments", distinct=True))
diff --git a/tests/foreign_object/test_tuple_lookups.py b/tests/foreign_object/test_tuple_lookups.py
index 797fea1c8a..42717c4f11 100644
--- a/tests/foreign_object/test_tuple_lookups.py
+++ b/tests/foreign_object/test_tuple_lookups.py
@@ -1,6 +1,5 @@
 import itertools
 
-from django.db import NotSupportedError
 from django.db.models import F
 from django.db.models.fields.tuple_lookups import (
     TupleExact,
@@ -65,7 +64,7 @@ class TupleLookupsTests(TestCase):
 
     def test_exact_subquery(self):
         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]
             self.assertSequenceEqual(
@@ -239,7 +238,7 @@ class TupleLookupsTests(TestCase):
 
     def test_lt_subquery(self):
         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]
             self.assertSequenceEqual(
@@ -287,7 +286,7 @@ class TupleLookupsTests(TestCase):
 
     def test_lte_subquery(self):
         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]
             self.assertSequenceEqual(
@@ -327,7 +326,7 @@ class TupleLookupsTests(TestCase):
 
     def test_gt_subquery(self):
         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]
             self.assertSequenceEqual(
@@ -375,7 +374,7 @@ class TupleLookupsTests(TestCase):
 
     def test_gte_subquery(self):
         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]
             self.assertSequenceEqual(
@@ -419,7 +418,7 @@ class TupleLookupsTests(TestCase):
 
     def test_isnull_subquery(self):
         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]
             self.assertSequenceEqual(