@@ -73,15 +73,27 @@ def _alter_field(
73
73
strict = False ,
74
74
):
75
75
collection = self .get_collection (model ._meta .db_table )
76
+ # Removed an index?
77
+ old_field_indexed = self ._field_should_be_indexed (model , old_field )
78
+ new_field_indexed = self ._field_should_be_indexed (model , new_field )
79
+ if old_field_indexed and not new_field_indexed :
80
+ self ._remove_field_index (model , old_field )
76
81
# Have they renamed the column?
77
82
if old_field .column != new_field .column :
78
83
collection .update_many ({}, {"$rename" : {old_field .column : new_field .column }})
84
+ # Move index to the new field, if needed.
85
+ if old_field_indexed and new_field_indexed :
86
+ self ._remove_field_index (model , old_field )
87
+ self ._add_field_index (model , new_field )
79
88
# Replace NULL with the field default if the field and was changed from
80
89
# NULL to NOT NULL.
81
90
if new_field .has_default () and old_field .null and not new_field .null :
82
91
column = new_field .column
83
92
default = self .effective_default (new_field )
84
93
collection .update_many ({column : {"$eq" : None }}, [{"$set" : {column : default }}])
94
+ # Added an index?
95
+ if not old_field_indexed and new_field_indexed :
96
+ self ._add_field_index (model , new_field )
85
97
86
98
def remove_field (self , model , field ):
87
99
# Remove implicit M2M tables.
0 commit comments