@@ -76,6 +76,9 @@ def _alter_field(
7676 strict = False ,
7777 ):
7878 collection = self .connection .database [model ._meta .db_table ]
79+ # Has unique been removed?
80+ if self ._field_should_have_unique (old_field ) and not self ._field_should_have_unique (new_field ):
81+ self ._remove_field_unique (model , old_field , strict )
7982 # Removed an index?
8083 if self ._field_should_be_indexed (model , old_field ) and not self ._field_should_be_indexed (
8184 model , new_field
@@ -90,6 +93,11 @@ def _alter_field(
9093 ):
9194 self ._remove_field_index (model , old_field )
9295 self ._add_field_index (model , new_field )
96+ # Move unique to the new field, if needed.
97+ if self ._field_should_have_unique (old_field ) and self ._field_should_have_unique (new_field ):
98+ if not old_field .primary_key :
99+ self ._remove_field_unique (model , old_field , strict )
100+ self ._add_field_unique (model , new_field )
93101 # Replace NULL with the field default if the field and was changed from
94102 # NULL to NOT NULL.
95103 if new_field .has_default () and old_field .null and not new_field .null :
@@ -101,6 +109,9 @@ def _alter_field(
101109 model , new_field
102110 ):
103111 self ._add_field_index (model , new_field )
112+ # Added a unique?
113+ if self ._unique_should_be_added (old_field , new_field ):
114+ self ._add_field_unique (model , new_field )
104115
105116 def remove_field (self , model , field ):
106117 # Remove implicit M2M tables.
0 commit comments