@@ -76,6 +76,9 @@ def _alter_field(
76
76
strict = False ,
77
77
):
78
78
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 )
79
82
# Removed an index?
80
83
if self ._field_should_be_indexed (model , old_field ) and not self ._field_should_be_indexed (
81
84
model , new_field
@@ -90,6 +93,11 @@ def _alter_field(
90
93
):
91
94
self ._remove_field_index (model , old_field )
92
95
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 )
93
101
# Replace NULL with the field default if the field and was changed from
94
102
# NULL to NOT NULL.
95
103
if new_field .has_default () and old_field .null and not new_field .null :
@@ -101,6 +109,9 @@ def _alter_field(
101
109
model , new_field
102
110
):
103
111
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 )
104
115
105
116
def remove_field (self , model , field ):
106
117
# Remove implicit M2M tables.
0 commit comments