From b35ccc6837409b0e378fe9a64bd9cc1bedfb0716 Mon Sep 17 00:00:00 2001 From: mShan0 <96149598+mShan0@users.noreply.github.com> Date: Fri, 24 May 2024 09:58:15 -0700 Subject: [PATCH] Revert "Remove deprecated meta.index_together field" This reverts commit b1e2a1660a583c18cb915b2df46dd7e43a581edb. --- mssql/schema.py | 10 +++++----- testapp/tests/test_indexes.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mssql/schema.py b/mssql/schema.py index 5ebf212d..0eff3d90 100644 --- a/mssql/schema.py +++ b/mssql/schema.py @@ -291,7 +291,7 @@ def alter_unique_together(self, model, old_unique_together, new_unique_together) def _model_indexes_sql(self, model): """ Return a list of all index SQL statements (field indexes, - Meta.indexes) for the specified model. + index_together, Meta.indexes) for the specified model. """ if not model._meta.managed or model._meta.proxy or model._meta.swapped: return [] @@ -299,7 +299,7 @@ def _model_indexes_sql(self, model): for field in model._meta.local_fields: output.extend(self._field_indexes_sql(model, field)) - for field_names in model._meta.indexes: + for field_names in model._meta.index_together: fields = [model._meta.get_field(field) for field in field_names] output.append(self._create_index_sql(model, fields, suffix="_idx")) @@ -792,7 +792,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, if old_field.db_index and new_field.db_index: index_columns.append([old_field]) else: - for fields in model._meta.indexes: + for fields in model._meta.index_together: columns = [model._meta.get_field(field) for field in fields] if old_field.column in [c.column for c in columns]: index_columns.append(columns) @@ -917,7 +917,7 @@ def _delete_indexes(self, model, old_field, new_field): index_columns.append([old_field.column]) elif old_field.null != new_field.null: index_columns.append([old_field.column]) - for fields in model._meta.indexes: + for fields in model._meta.index_together: columns = [model._meta.get_field(field).column for field in fields] if old_field.column in columns: index_columns.append(columns) @@ -1322,7 +1322,7 @@ def create_model(self, model): model, field, field_type, field.db_comment ) ) - # Add any field index and indexes's (deferred as SQLite3 _remake_table needs it) + # Add any field index and index_together's (deferred as SQLite3 _remake_table needs it) self.deferred_sql.extend(self._model_indexes_sql(model)) self.deferred_sql = list(set(self.deferred_sql)) diff --git a/testapp/tests/test_indexes.py b/testapp/tests/test_indexes.py index 9859fb03..53e7ec38 100644 --- a/testapp/tests/test_indexes.py +++ b/testapp/tests/test_indexes.py @@ -78,7 +78,7 @@ class TestCorrectIndexes(TestCase): def test_correct_indexes_exist(self): """ Check there are the correct number of indexes for each field after all migrations - by comparing what the model says (e.g. `db_index=True` / `indexes` etc.) + by comparing what the model says (e.g. `db_index=True` / `index_together` etc.) with the actual constraints found in the database. This acts as a general regression test for issues such as: - duplicate index created (e.g. https://github.com/microsoft/mssql-django/issues/77) @@ -115,9 +115,9 @@ def test_correct_indexes_exist(self): expected_index_causes = [] if field.db_index: expected_index_causes.append('db_index=True') - for field_names in model_cls._meta.indexes: + for field_names in model_cls._meta.index_together: if field.name in field_names: - expected_index_causes.append(f'indexes[{field_names}]') + expected_index_causes.append(f'index_together[{field_names}]') if field._unique and field.null: # This is implemented using a (filtered) unique index (not a constraint) to get ANSI NULL behaviour expected_index_causes.append('unique=True & null=True')