@@ -88,7 +88,14 @@ def remove_field(self, model, field):
88
88
self ._remove_field_index (model , field )
89
89
90
90
def alter_index_together (self , model , old_index_together , new_index_together ):
91
- pass
91
+ olds = {tuple (fields ) for fields in old_index_together }
92
+ news = {tuple (fields ) for fields in new_index_together }
93
+ # Deleted indexes
94
+ for field_names in olds .difference (news ):
95
+ self ._remove_composed_index (model , field_names , {"index" : True , "unique" : False })
96
+ # Created indexes
97
+ for field_names in news .difference (olds ):
98
+ self ._add_composed_index (model , field_names )
92
99
93
100
def alter_unique_together (self , model , old_unique_together , new_unique_together ):
94
101
pass
@@ -127,6 +134,30 @@ def remove_index(self, model, index):
127
134
return
128
135
self .connection .database [model ._meta .db_table ].drop_index (index .name )
129
136
137
+ def _remove_composed_index (self , model , field_names , constraint_kwargs ):
138
+ """
139
+ Remove the index on the given list of field_names created by
140
+ index/unique_together, depending on the constraint_kwargs.
141
+ """
142
+ meta_constraint_names = {constraint .name for constraint in model ._meta .constraints }
143
+ meta_index_names = {constraint .name for constraint in model ._meta .indexes }
144
+ columns = [model ._meta .get_field (field ).column for field in field_names ]
145
+ constraint_names = self ._constraint_names (
146
+ model ,
147
+ columns ,
148
+ exclude = meta_constraint_names | meta_index_names ,
149
+ ** constraint_kwargs ,
150
+ )
151
+ if len (constraint_names ) != 1 :
152
+ num_found = len (constraint_names )
153
+ columns_str = ", " .join (columns )
154
+ raise ValueError (
155
+ f"Found wrong number ({ num_found } ) of constraints for "
156
+ f"{ model ._meta .db_table } ({ columns_str } )."
157
+ )
158
+ collection = self .connection .database [model ._meta .db_table ]
159
+ collection .drop_index (constraint_names [0 ])
160
+
130
161
def add_constraint (self , model , constraint ):
131
162
pass
132
163
0 commit comments