diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 6afe68bac0..ccb67876e2 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -7,6 +7,7 @@ from django.core.exceptions import EmptyResultSet, FieldError from django.db import connection from django.db.models import fields from django.db.models.query_utils import Q +from django.db.utils import NotSupportedError from django.utils.deconstruct import deconstructible from django.utils.functional import cached_property from django.utils.hashable import make_hashable @@ -1237,6 +1238,8 @@ class Window(Expression): def as_sql(self, compiler, connection, template=None): connection.ops.check_expression_support(self) + if not connection.features.supports_over_clause: + raise NotSupportedError('This backend does not support window expressions.') expr_sql, params = compiler.compile(self.source_expression) window_sql, window_params = [], [] diff --git a/tests/expressions_window/tests.py b/tests/expressions_window/tests.py index fa9bb2916b..a993a5e575 100644 --- a/tests/expressions_window/tests.py +++ b/tests/expressions_window/tests.py @@ -1,5 +1,5 @@ import datetime -from unittest import skipIf, skipUnless +from unittest import mock, skipIf, skipUnless from django.core.exceptions import FieldError from django.db import NotSupportedError, connection @@ -821,6 +821,12 @@ class NonQueryWindowTests(SimpleTestCase): with self.assertRaisesMessage(NotSupportedError, msg): Employee.objects.annotate(dense_rank=Window(expression=DenseRank())).filter(dense_rank__gte=1) + def test_unsupported_backend(self): + msg = 'This backend does not support window expressions.' + with mock.patch.object(connection.features, 'supports_over_clause', False): + with self.assertRaisesMessage(NotSupportedError, msg): + Employee.objects.annotate(dense_rank=Window(expression=DenseRank())).get() + def test_invalid_order_by(self): msg = 'order_by must be either an Expression or a sequence of expressions' with self.assertRaisesMessage(ValueError, msg):