@@ -30,7 +30,8 @@ def create_model(self, model):
30
30
def _create_model_indexes (self , model ):
31
31
"""
32
32
Create all indexes (field indexes & uniques, Meta.index_together,
33
- Meta.constraints, Meta.indexes) for the specified model.
33
+ Meta.unique_together, Meta.constraints, Meta.indexes) for the specified
34
+ model.
34
35
"""
35
36
if not model ._meta .managed or model ._meta .proxy or model ._meta .swapped :
36
37
return
@@ -43,6 +44,9 @@ def _create_model_indexes(self, model):
43
44
# Meta.index_together (RemovedInDjango51Warning)
44
45
for field_names in model ._meta .index_together :
45
46
self ._add_composed_index (model , field_names )
47
+ # Meta.unique_together
48
+ if model ._meta .unique_together :
49
+ self .alter_unique_together (model , [], model ._meta .unique_together )
46
50
# Meta.constraints
47
51
for constraint in model ._meta .constraints :
48
52
self .add_constraint (model , constraint )
@@ -131,7 +135,28 @@ def alter_index_together(self, model, old_index_together, new_index_together):
131
135
self ._add_composed_index (model , field_names )
132
136
133
137
def alter_unique_together (self , model , old_unique_together , new_unique_together ):
134
- pass
138
+ olds = {tuple (fields ) for fields in old_unique_together }
139
+ news = {tuple (fields ) for fields in new_unique_together }
140
+ # Deleted uniques
141
+ for field_names in olds .difference (news ):
142
+ self ._remove_composed_index (
143
+ model ,
144
+ field_names ,
145
+ {"unique" : True , "primary_key" : False },
146
+ )
147
+ # Created uniques
148
+ for field_names in news .difference (olds ):
149
+ fields = [model ._meta .get_field (field ) for field in field_names ]
150
+ name = str (
151
+ self ._unique_constraint_name (
152
+ model ._meta .db_table , [field .column for field in fields ]
153
+ )
154
+ )
155
+ constraint = UniqueConstraint (
156
+ fields = field_names ,
157
+ name = name ,
158
+ )
159
+ self .add_constraint (model , constraint )
135
160
136
161
def add_index (self , model , index , field = None , unique = False ):
137
162
if index .contains_expressions :
0 commit comments