From e6748e0bace5df7274e446e60a7d68979e99f64b Mon Sep 17 00:00:00 2001 From: dauinsight <145612907+dauinsight@users.noreply.github.com> Date: Fri, 22 Dec 2023 10:03:26 -0800 Subject: [PATCH] Implement fix for empty aggregate (#324) --- mssql/compiler.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mssql/compiler.py b/mssql/compiler.py index 2dfe1b6c..e2e0b703 100644 --- a/mssql/compiler.py +++ b/mssql/compiler.py @@ -16,7 +16,7 @@ if django.VERSION >= (3, 1): from django.db.models.fields.json import compile_json_path, KeyTransform as json_KeyTransform if django.VERSION >= (4, 2): - from django.core.exceptions import FullResultSet + from django.core.exceptions import EmptyResultSet, FullResultSet def _as_sql_agv(self, compiler, connection): return self.as_sql(compiler, connection, template='%(function)s(CONVERT(float, %(field)s))') @@ -241,6 +241,11 @@ def as_sql(self, with_limits=True, with_col_aliases=False): if django.VERSION >= (4, 2): try: where, w_params = self.compile(self.where) if self.where is not None else ("", []) + except EmptyResultSet: + if self.elide_empty: + raise + # Use a predicate that's always False. + where, w_params = "0 = 1", [] except FullResultSet: where, w_params = "", [] try: