@@ -292,17 +292,24 @@ def alter_unique_together(self, model, old_unique_together, new_unique_together)
292292 def _model_indexes_sql (self , model ):
293293 """
294294 Return a list of all index SQL statements (field indexes,
295- index_together, Meta.indexes) for the specified model.
295+ Meta.indexes) for the specified model.
296296 """
297297 if not model ._meta .managed or model ._meta .proxy or model ._meta .swapped :
298298 return []
299299 output = []
300300 for field in model ._meta .local_fields :
301301 output .extend (self ._field_indexes_sql (model , field ))
302-
303- for field_names in model ._meta .index_together :
304- fields = [model ._meta .get_field (field ) for field in field_names ]
305- output .append (self ._create_index_sql (model , fields , suffix = "_idx" ))
302+ # meta.index_together is removed in Django 5.1, so add a version check to handle compatibility
303+ if django_version < (5 , 1 ):
304+ # Iterate over each set of field names defined in index_together
305+ for field_names in model ._meta .index_together :
306+ # Get the actual field objects for each field name
307+ fields = [model ._meta .get_field (field ) for field in field_names ]
308+ # Generate the SQL statement to create the index for these fields
309+ sql = self ._create_index_sql (model , fields , suffix = "_idx" )
310+ # If SQL was generated (not None), add it to the output list
311+ if sql :
312+ output .append (sql )
306313
307314 if django_version >= (4 , 0 ):
308315 for field_names in model ._meta .unique_together :
@@ -803,10 +810,15 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
803810 if old_field .db_index and new_field .db_index :
804811 index_columns .append ([old_field ])
805812 else :
806- for fields in model ._meta .index_together :
807- columns = [model ._meta .get_field (field ) for field in fields ]
808- if old_field .column in [c .column for c in columns ]:
809- index_columns .append (columns )
813+ # Handle index_together for only django version < 5.1
814+ if django_version < (5 , 1 ):
815+ # Get the field objects for each field name in the index_together.
816+ for fields in model ._meta .index_together :
817+ # If the old field's column is among the columns for this index,
818+ # add this set of columns to index_columns for later index recreation.
819+ columns = [model ._meta .get_field (field ) for field in fields ]
820+ if old_field .column in [c .column for c in columns ]:
821+ index_columns .append (columns )
810822 if index_columns :
811823 for columns in index_columns :
812824 create_index_sql_statement = self ._create_index_sql (model , columns )
@@ -935,10 +947,15 @@ def _delete_indexes(self, model, old_field, new_field):
935947 index_columns .append ([old_field .column ])
936948 elif old_field .null != new_field .null :
937949 index_columns .append ([old_field .column ])
938- for fields in model ._meta .index_together :
939- columns = [model ._meta .get_field (field ).column for field in fields ]
940- if old_field .column in columns :
941- index_columns .append (columns )
950+ # Handle index_together for only django version < 5.1
951+ if django_version < (5 , 1 ):
952+ # Iterate over each set of field names defined in index_together
953+ for fields in model ._meta .index_together :
954+ # Get the actual column names for each field in the set
955+ columns = [model ._meta .get_field (field ).column for field in fields ]
956+ # If the old field's column is among these columns, add to index_columns for later index deletion
957+ if old_field .column in columns :
958+ index_columns .append (columns )
942959
943960 for index in model ._meta .indexes :
944961 columns = [model ._meta .get_field (field ).column for field in index .fields ]
0 commit comments