diff --git a/mssql/introspection.py b/mssql/introspection.py index b69efd58..2ae3a3b9 100644 --- a/mssql/introspection.py +++ b/mssql/introspection.py @@ -13,9 +13,11 @@ SQL_AUTOFIELD = -777555 SQL_BIGAUTOFIELD = -777444 + def get_schema_name(): return getattr(settings, 'SCHEMA_TO_INSPECT', 'SCHEMA_NAME()') + class DatabaseIntrospection(BaseDatabaseIntrospection): # Map type codes to Django Field types. data_types_reverse = { @@ -66,7 +68,8 @@ def get_table_list(self, cursor): """ Returns a list of table and view names in the current database. """ - sql = f'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = {get_schema_name()}' + sql = 'SELECT TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s' % ( + get_schema_name()) cursor.execute(sql) types = {'BASE TABLE': 't', 'VIEW': 'v'} return [TableInfo(row[0], types.get(row[1])) @@ -114,7 +117,7 @@ def get_table_description(self, cursor, table_name, identity_check=True): """ % (table_name, column[0]) cursor.execute(sql) collation_name = cursor.fetchone() - column.append(collation_name[0] if collation_name else '') + column.append(collation_name[0] if collation_name else '') else: column.append('') diff --git a/mssql/schema.py b/mssql/schema.py index 7cdac09d..9b7fb9f3 100644 --- a/mssql/schema.py +++ b/mssql/schema.py @@ -392,7 +392,8 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, result = cursor.fetchall() columns_to_recreate_index = ', '.join(['%s' % self.quote_name(column[0]) for column in result]) filter_definition = result[0][1] - sql_restore_index += f'CREATE UNIQUE INDEX {index_name} ON {model._meta.db_table} ({columns_to_recreate_index}) WHERE {filter_definition};' + sql_restore_index += 'CREATE UNIQUE INDEX %s ON %s (%s) WHERE %s;' % ( + index_name, model._meta.db_table, columns_to_recreate_index, filter_definition) self.execute(self._db_table_delete_constraint_sql( self.sql_delete_index, model._meta.db_table, index_name)) self.execute(self._rename_field_sql(model._meta.db_table, old_field, new_field, new_type)) @@ -453,7 +454,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, (old_field.db_index or not new_field.db_index) and new_field.db_index or ((indexes_dropped and sorted(indexes_dropped) == sorted([index.name for index in model._meta.indexes])) or - (indexes_dropped and sorted(indexes_dropped) == sorted(auto_index_names))) + (indexes_dropped and sorted(indexes_dropped) == sorted(auto_index_names))) ): create_index_sql_statement = self._create_index_sql(model, [new_field]) if create_index_sql_statement.__str__() not in [sql.__str__() for sql in self.deferred_sql]: @@ -580,8 +581,8 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, for columns in index_columns: create_index_sql_statement = self._create_index_sql(model, columns) if (create_index_sql_statement.__str__() - not in [sql.__str__() for sql in self.deferred_sql] + [statement[0].__str__() for statement in post_actions] - ): + not in [sql.__str__() for sql in self.deferred_sql] + [statement[0].__str__() for statement in post_actions] + ): self.execute(create_index_sql_statement) # Type alteration on primary key? Then we need to alter the column