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