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