@@ -88,6 +88,11 @@ def _alter_field(
88
88
strict = False ,
89
89
):
90
90
collection = self .get_collection (model ._meta .db_table )
91
+ # Has unique been removed?
92
+ old_field_unique = self ._field_should_have_unique (old_field )
93
+ new_field_unique = self ._field_should_have_unique (new_field )
94
+ if old_field_unique and not new_field_unique :
95
+ self ._remove_field_unique (model , old_field )
91
96
# Removed an index?
92
97
old_field_indexed = self ._field_should_be_indexed (model , old_field )
93
98
new_field_indexed = self ._field_should_be_indexed (model , new_field )
@@ -100,6 +105,10 @@ def _alter_field(
100
105
if old_field_indexed and new_field_indexed :
101
106
self ._remove_field_index (model , old_field )
102
107
self ._add_field_index (model , new_field )
108
+ # Move unique to the new field, if needed.
109
+ if old_field_unique and new_field_unique :
110
+ self ._remove_field_unique (model , old_field )
111
+ self ._add_field_unique (model , new_field )
103
112
# Replace NULL with the field default if the field and was changed from
104
113
# NULL to NOT NULL.
105
114
if new_field .has_default () and old_field .null and not new_field .null :
@@ -109,6 +118,9 @@ def _alter_field(
109
118
# Added an index?
110
119
if not old_field_indexed and new_field_indexed :
111
120
self ._add_field_index (model , new_field )
121
+ # Added a unique?
122
+ if not old_field_unique and new_field_unique :
123
+ self ._add_field_unique (model , new_field )
112
124
113
125
def remove_field (self , model , field ):
114
126
# Remove implicit M2M tables.
0 commit comments