@@ -89,6 +89,9 @@ 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
+ if self ._field_should_have_unique (old_field ) and not self ._field_should_have_unique (new_field ):
94
+ self ._remove_field_unique (model , old_field , strict )
92
95
# Removed an index?
93
96
old_field_indexed = self ._field_should_be_indexed (model , old_field )
94
97
new_field_indexed = self ._field_should_be_indexed (model , new_field )
@@ -101,6 +104,11 @@ def _alter_field(
101
104
if old_field_indexed and new_field_indexed :
102
105
self ._remove_field_index (model , old_field )
103
106
self ._add_field_index (model , new_field )
107
+ # Move unique to the new field, if needed.
108
+ if self ._field_should_have_unique (old_field ) and self ._field_should_have_unique (new_field ):
109
+ if not old_field .primary_key :
110
+ self ._remove_field_unique (model , old_field , strict )
111
+ self ._add_field_unique (model , new_field )
104
112
# Replace NULL with the field default if the field and was changed from
105
113
# NULL to NOT NULL.
106
114
if new_field .has_default () and old_field .null and not new_field .null :
@@ -110,6 +118,9 @@ def _alter_field(
110
118
# Added an index?
111
119
if not old_field_indexed and new_field_indexed :
112
120
self ._add_field_index (model , new_field )
121
+ # Added a unique?
122
+ if self ._unique_should_be_added (old_field , new_field ):
123
+ self ._add_field_unique (model , new_field )
113
124
114
125
def remove_field (self , model , field ):
115
126
# Remove implicit M2M tables.
0 commit comments