简体   繁体   中英

Is it safe to rename Django migrations file?

Since Django 1.8 the makemigrations command has a --name, -n option to specify custom name for the created migrations file.

I'd like to know whether it's safe in older versions of Django to create the migrations file with the automatically generated name and then rename the file manually. It seems to work as expected. Are there any potential risks?

This works, with a minor caveat: Django will no longer know that the renamed migration is applied.

So the steps to renaming a migration are:

  1. Rename the file.
  2. Repoint any dependencies to the new file.
  3. If the renamed migration was already applied, apply it again using --fake .

If it's a brand new migration, 2 and 3 won't apply, and it's perfectly fine to rename them.

This is happens in Django every time migrations are squashed. A new file is generated thats contains the class variable replaces , this lists the migration files that are being replaced.

So to rename a file migration file add in the following variable in the Migration class:

replaces = [('app name', 'migration file name'), ]

And everything works like it did before the file change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM