From 81018de54c7669ba64d2ddac9927f3476da9d6db Mon Sep 17 00:00:00 2001 From: Khanh Bui <85855766+khanhmaibui@users.noreply.github.com> Date: Tue, 21 Feb 2023 12:22:00 -0800 Subject: [PATCH] Fix errors with raising FullResultSet exception and with alter_column_type_sql() and collate_sql() functions (#229) * fix error with raising fullresultset * add django4.2 condition * fix alter_column_type_sql and collate_sql to take 2 additional arguments * delete argument 'old_rel_collation' * fix arguments names --- mssql/compiler.py | 16 ++++++++++++++-- mssql/schema.py | 37 ++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/mssql/compiler.py b/mssql/compiler.py index e71aa14f..91ff9bc1 100644 --- a/mssql/compiler.py +++ b/mssql/compiler.py @@ -15,6 +15,8 @@ from django.db.utils import NotSupportedError 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 def _as_sql_agv(self, compiler, connection): return self.as_sql(compiler, connection, template='%(function)s(CONVERT(float, %(field)s))') @@ -233,8 +235,18 @@ def as_sql(self, with_limits=True, with_col_aliases=False): # This must come after 'select', 'ordering', and 'distinct' -- see # docstring of get_from_clause() for details. from_, f_params = self.get_from_clause() - where, w_params = self.compile(self.where) if self.where is not None else ("", []) - having, h_params = self.compile(self.having) if self.having is not None else ("", []) + if django.VERSION >= (4, 2): + try: + where, w_params = self.compile(self.where) if self.where is not None else ("", []) + except FullResultSet: + where, w_params = "", [] + try: + having, h_params = self.compile(self.having) if self.having is not None else ("", []) + except FullResultSet: + having, h_params = "", [] + else: + where, w_params = self.compile(self.where) if self.where is not None else ("", []) + having, h_params = self.compile(self.having) if self.having is not None else ("", []) params = [] result = ['SELECT'] diff --git a/mssql/schema.py b/mssql/schema.py index f977fdc0..00ee5403 100644 --- a/mssql/schema.py +++ b/mssql/schema.py @@ -161,9 +161,14 @@ def _alter_column_null_sql(self, model, old_field, new_field): [], ) - def _alter_column_type_sql(self, model, old_field, new_field, new_type): - new_type = self._set_field_new_type_null_status(old_field, new_type) - return super()._alter_column_type_sql(model, old_field, new_field, new_type) + if django_version >= (4, 2): + def _alter_column_type_sql(self, model, old_field, new_field, new_type, old_collation, new_collation): + new_type = self._set_field_new_type_null_status(old_field, new_type) + return super()._alter_column_type_sql(model, old_field, new_field, new_type, old_collation, new_collation) + else: + def _alter_column_type_sql(self, model, old_field, new_field, new_type): + new_type = self._set_field_new_type_null_status(old_field, new_type) + return super()._alter_column_type_sql(model, old_field, new_field, new_type) def alter_unique_together(self, model, old_unique_together, new_unique_together): """ @@ -443,7 +448,12 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, post_actions = [] # Type change? if old_type != new_type: - fragment, other_actions = self._alter_column_type_sql(model, old_field, new_field, new_type) + if django_version >= (4, 2): + fragment, other_actions = self._alter_column_type_sql( + model, old_field, new_field, new_type, old_collation=None, new_collation=None + ) + else: + fragment, other_actions = self._alter_column_type_sql(model, old_field, new_field, new_type) actions.append(fragment) post_actions.extend(other_actions) # Drop unique constraint, SQL Server requires explicit deletion @@ -683,9 +693,14 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, for old_rel, new_rel in rels_to_update: rel_db_params = new_rel.field.db_parameters(connection=self.connection) rel_type = rel_db_params['type'] - fragment, other_actions = self._alter_column_type_sql( - new_rel.related_model, old_rel.field, new_rel.field, rel_type - ) + if django_version >= (4, 2): + fragment, other_actions = self._alter_column_type_sql( + new_rel.related_model, old_rel.field, new_rel.field, rel_type, old_collation=None, new_collation=None + ) + else: + fragment, other_actions = self._alter_column_type_sql( + new_rel.related_model, old_rel.field, new_rel.field, rel_type + ) # Drop related_model indexes, so it can be altered index_names = self._db_table_constraint_names(old_rel.related_model._meta.db_table, index=True) for index_name in index_names: @@ -1262,8 +1277,12 @@ def add_constraint(self, model, constraint): (constraint.condition.connector, constraint.name)) super().add_constraint(model, constraint) - def _collate_sql(self, collation): - return ' COLLATE ' + collation + if django_version >= (4, 2): + def _collate_sql(self, collation, old_collation=None, table_name=None): + return ' COLLATE ' + collation if collation else "" + else: + def _collate_sql(self, collation): + return ' COLLATE ' + collation def _create_index_name(self, table_name, column_names, suffix=""): index_name = super()._create_index_name(table_name, column_names, suffix)