|
4 | 4 | from django.db.backends.base.schema import ( |
5 | 5 | BaseDatabaseSchemaEditor, logger, _related_non_m2m_objects, |
6 | 6 | ) |
| 7 | +from django.db.models import Index |
7 | 8 | from django.db.models.fields import AutoField, BigAutoField |
8 | 9 | from django.db.models.fields.related import ManyToManyField |
9 | 10 | from django.db.transaction import TransactionManagementError |
@@ -111,8 +112,16 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type, |
111 | 112 | # True | False | True | True |
112 | 113 | if old_field.db_index and not old_field.unique and (not new_field.db_index or new_field.unique): |
113 | 114 | # Find the index for this field |
114 | | - index_names = self._constraint_names(model, [old_field.column], index=True) |
| 115 | + meta_index_names = {index.name for index in model._meta.indexes} |
| 116 | + # Retrieve only BTREE indexes since this is what's created with |
| 117 | + # db_index=True. |
| 118 | + index_names = self._constraint_names(model, [old_field.column], index=True, type_=Index.suffix) |
115 | 119 | for index_name in index_names: |
| 120 | + if index_name in meta_index_names: |
| 121 | + # The only way to check if an index was created with |
| 122 | + # db_index=True or with Index(['field'], name='foo') |
| 123 | + # is to look at its name (refs #28053). |
| 124 | + continue |
116 | 125 | self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name)) |
117 | 126 | # Change check constraints? |
118 | 127 | if old_db_params['check'] != new_db_params['check'] and old_db_params['check']: |
|
0 commit comments