diff options
| author | Adam Johnson <me@adamj.eu> | 2026-02-11 23:25:47 +0000 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-02-12 15:27:11 -0500 |
| commit | 055d7a682f4923fc5a50d7c1a12fd7f52675f7e8 (patch) | |
| tree | f0f412259d1bf42a81f7ee22f6d485caba35a960 | |
| parent | 0dd9d5ee5c30e51754d93e926e24ff92654ecf5d (diff) | |
Improved error message in SQLite `DatabaseOperations.check_expression_support()`.
| -rw-r--r-- | django/db/backends/sqlite3/operations.py | 6 | ||||
| -rw-r--r-- | tests/backends/sqlite/tests.py | 12 |
2 files changed, 11 insertions, 7 deletions
diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index 33a39239ca..949cbd4ff9 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -48,10 +48,10 @@ class DatabaseOperations(BaseDatabaseOperations): pass else: if isinstance(output_field, DATETIME_FIELDS): + klass = expression.__class__.__name__ raise NotSupportedError( - "You cannot use Sum, Avg, StdDev, and Variance " - "aggregations on date/time fields in sqlite3 " - "since date/time is saved as text." + f"SQLite does not support {klass} on date or time " + "fields, because they are stored as text." ) if ( isinstance(expression, models.Aggregate) diff --git a/tests/backends/sqlite/tests.py b/tests/backends/sqlite/tests.py index 34c0eca0ff..f47e96be4e 100644 --- a/tests/backends/sqlite/tests.py +++ b/tests/backends/sqlite/tests.py @@ -31,13 +31,17 @@ class Tests(TestCase): def test_aggregation(self): """Raise NotSupportedError when aggregating on date/time fields.""" for aggregate in (Sum, Avg, Variance, StdDev): - with self.assertRaises(NotSupportedError): + msg = ( + f"SQLite does not support {aggregate.__name__} on date or " + "time fields, because they are stored as text." + ) + with self.assertRaisesMessage(NotSupportedError, msg): Item.objects.aggregate(aggregate("time")) - with self.assertRaises(NotSupportedError): + with self.assertRaisesMessage(NotSupportedError, msg): Item.objects.aggregate(aggregate("date")) - with self.assertRaises(NotSupportedError): + with self.assertRaisesMessage(NotSupportedError, msg): Item.objects.aggregate(aggregate("last_modified")) - with self.assertRaises(NotSupportedError): + with self.assertRaisesMessage(NotSupportedError, msg): Item.objects.aggregate( **{ "complex": aggregate("last_modified") |
