From f9688612ce513a05e30771a2dbc22abfe714617d Mon Sep 17 00:00:00 2001 From: Khanh Bui Date: Thu, 16 Feb 2023 14:41:15 -0800 Subject: [PATCH 1/5] fix error with raising fullresultset --- mssql/compiler.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mssql/compiler.py b/mssql/compiler.py index e71aa14f..c8d0d2de 100644 --- a/mssql/compiler.py +++ b/mssql/compiler.py @@ -5,6 +5,7 @@ from itertools import chain import django +from django.core.exceptions import FullResultSet from django.db.models.aggregates import Avg, Count, StdDev, Variance from django.db.models.expressions import Ref, Subquery, Value, Window from django.db.models.functions import ( @@ -233,8 +234,14 @@ 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 ("", []) + 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 = "", [] params = [] result = ['SELECT'] From 3f6c4b6ca12be9682bd287440fbe4ba74e777790 Mon Sep 17 00:00:00 2001 From: Khanh Bui Date: Thu, 16 Feb 2023 15:23:05 -0800 Subject: [PATCH 2/5] add django4.2 condition --- mssql/compiler.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mssql/compiler.py b/mssql/compiler.py index c8d0d2de..91ff9bc1 100644 --- a/mssql/compiler.py +++ b/mssql/compiler.py @@ -5,7 +5,6 @@ from itertools import chain import django -from django.core.exceptions import FullResultSet from django.db.models.aggregates import Avg, Count, StdDev, Variance from django.db.models.expressions import Ref, Subquery, Value, Window from django.db.models.functions import ( @@ -16,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))') @@ -234,14 +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() - try: + 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 ("", []) - 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 = "", [] params = [] result = ['SELECT'] From f4650ffb1f26240d987d737ff0efd442bad570cc Mon Sep 17 00:00:00 2001 From: Khanh Bui Date: Thu, 16 Feb 2023 15:52:49 -0800 Subject: [PATCH 3/5] fix alter_column_type_sql and collate_sql to take 2 additional arguments --- mssql/schema.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/mssql/schema.py b/mssql/schema.py index f977fdc0..88386205 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,15 @@ 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_rel_collation=None, rel_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 +1278,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) From 25b2f77ec337dc367fc94018180687648ee0a852 Mon Sep 17 00:00:00 2001 From: Khanh Bui Date: Thu, 16 Feb 2023 16:03:12 -0800 Subject: [PATCH 4/5] delete argument 'old_rel_collation' --- mssql/schema.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mssql/schema.py b/mssql/schema.py index 88386205..4984d2cd 100644 --- a/mssql/schema.py +++ b/mssql/schema.py @@ -695,8 +695,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, rel_type = rel_db_params['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_rel_collation=None, rel_collation=None + new_rel.related_model, old_rel.field, new_rel.field, rel_type, None, None ) else: fragment, other_actions = self._alter_column_type_sql( From 9afe407f6ba23998c5d5c54f9be816f7bc1594dd Mon Sep 17 00:00:00 2001 From: Khanh Bui Date: Fri, 17 Feb 2023 13:29:30 -0800 Subject: [PATCH 5/5] fix arguments names --- mssql/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mssql/schema.py b/mssql/schema.py index 4984d2cd..00ee5403 100644 --- a/mssql/schema.py +++ b/mssql/schema.py @@ -695,7 +695,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, rel_type = rel_db_params['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, None, None + 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(