Opened 3 years ago

Last modified 3 years ago

#27064 new New feature

Implement RenameIndex in a backwards compatible way

Reported by: Markus Holtermann Owned by: nobody
Component: Migrations Version: master
Severity: Normal Keywords:
Cc: Andrew Godwin, Akshesh Doshi Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


In order to eventually deprecate index_together we need a way to deal with old projects that have unnamed indexes. This proves to be a non-trivial problem. Andrew and I came up with these things to consider.

  • RenameIndex(model, new_name, old_name=None, old_fields=None) where exactly one of old_name and old_field is given (old_name ^ old_fields)
  • If the old_name is given we use RENAME INDEX if available
  • Otherwise look at the state and drop existing indexes and create new the index with new name
  • On MySQL (or other DBs) that don't support RENAME INDEX, provide SQL query to look up index name from information_schema by field names and pass in to DROP INDEX.
  • If more than one index is found while identifying with field names, migrations must error out with an AmbiguityError
  • If the autodetector finds an old, unnamed index and a new, named one matching field signature, issue a RenameIndex operation
  • For backwards operations with unnamed old indexes, RenameIndex is a noop.

Change History (5)

comment:1 Changed 3 years ago by Markus Holtermann

Type: UncategorizedNew feature

comment:2 Changed 3 years ago by Markus Holtermann

Cc: Akshesh Doshi added

comment:3 Changed 3 years ago by Markus Holtermann

Cc: Andrew Godwin added; anr removed

comment:4 Changed 3 years ago by Akshesh Doshi

Is there a reason why we cannot just simply deprecate index_together.

My understanding is that the models file of the app will get modified from this

    class Meta:
        index_together = (('a', 'b'))

to this

    class Meta:
        indexes = [models.Index(fields=['a', 'b'])]

and everything should work fine (or not? Am I missing something?)

I need some help to see where RenameIndex comes into picture here.

comment:5 Changed 3 years ago by Markus Holtermann

I just noticed that I actually never commented here. So, the reason is explained here:

Note: See TracTickets for help on using tickets.
Back to Top