@@ -66,15 +66,27 @@ def _alter_field(
6666        strict = False ,
6767    ):
6868        collection  =  self .connection .database [model ._meta .db_table ]
69+         # Removed an index? 
70+         old_field_indexed  =  self ._field_should_be_indexed (model , old_field )
71+         new_field_indexed  =  self ._field_should_be_indexed (model , new_field )
72+         if  old_field_indexed  and  not  new_field_indexed :
73+             self ._remove_field_index (model , old_field )
6974        # Have they renamed the column? 
7075        if  old_field .column  !=  new_field .column :
7176            collection .update_many ({}, {"$rename" : {old_field .column : new_field .column }})
77+             # Move index to the new field, if needed. 
78+             if  old_field_indexed  and  new_field_indexed :
79+                 self ._remove_field_index (model , old_field )
80+                 self ._add_field_index (model , new_field )
7281        # Replace NULL with the field default if the field and was changed from 
7382        # NULL to NOT NULL. 
7483        if  new_field .has_default () and  old_field .null  and  not  new_field .null :
7584            column  =  new_field .column 
7685            default  =  self .effective_default (new_field )
7786            collection .update_many ({column : {"$eq" : None }}, [{"$set" : {column : default }}])
87+         # Added an index? 
88+         if  not  old_field_indexed  and  new_field_indexed :
89+             self ._add_field_index (model , new_field )
7890
7991    def  remove_field (self , model , field ):
8092        # Remove implicit M2M tables. 
@@ -155,6 +167,27 @@ def _remove_composed_index(self, model, field_names, constraint_kwargs):
155167        collection  =  self .connection .database [model ._meta .db_table ]
156168        collection .drop_index (constraint_names [0 ])
157169
170+     def  _remove_field_index (self , model , field ):
171+         """Remove a field's db_index=True index.""" 
172+         collection  =  self .connection .database [model ._meta .db_table ]
173+         meta_index_names  =  {index .name  for  index  in  model ._meta .indexes }
174+         index_names  =  self ._constraint_names (
175+             model ,
176+             [field .column ],
177+             index = True ,
178+             # Retrieve only BTREE indexes since this is what's created with 
179+             # db_index=True. 
180+             type_ = Index .suffix ,
181+             exclude = meta_index_names ,
182+         )
183+         if  len (index_names ) !=  1 :
184+             num_found  =  len (index_names )
185+             raise  ValueError (
186+                 f"Found wrong number ({ num_found }  
187+                 f"{ model ._meta .db_table } { field .column }  
188+             )
189+         collection .drop_index (index_names [0 ])
190+ 
158191    def  add_constraint (self , model , constraint ):
159192        pass 
160193
0 commit comments