Since Django 1.8 the makemigrations
command has a --name, -n
option to specify custom name for the created migrations file.
从Django 1.8开始,makemigrations命令具有--name,-n选项,用于为创建的迁移文件指定自定义名称。
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?
我想知道在旧版本的Django中使用自动生成的名称创建迁移文件是否安全,然后手动重命名该文件。它似乎按预期工作。有潜在的风险吗?
2 个解决方案
#1
18
This works, with a minor caveat: Django will no longer know that the renamed migration is applied.
这是有效的,但有一点需要注意:Django将不再知道应用了重命名的迁移。
So the steps to renaming a migration are:
因此,重命名迁移的步骤如下:
- Rename the file.
- 重命名文件。
- Repoint any dependencies to the new file.
- 重新指向新文件的所有依赖项。
- If the renamed migration was already applied, apply it again using
--fake
. - 如果已应用重命名的迁移,请使用--fake再次应用它。
If it's a brand new migration, 2 and 3 won't apply, and it's perfectly fine to rename them.
如果它是一个全新的迁移,2和3将不适用,并且重命名它们是完全正常的。
#2
11
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.
每次迁移被压扁时,都会在Django中发生这种情况。生成包含类变量替换的新文件,该文件列出了要替换的迁移文件。
So to rename a file migration file add in the following variable in the Migration class:
因此,要重命名文件迁移文件,请在Migration类中添加以下变量:
replaces = [('app name', 'migration file name'), ]
And everything works like it did before the file change.
一切都像文件更改之前一样。
#1
18
This works, with a minor caveat: Django will no longer know that the renamed migration is applied.
这是有效的,但有一点需要注意:Django将不再知道应用了重命名的迁移。
So the steps to renaming a migration are:
因此,重命名迁移的步骤如下:
- Rename the file.
- 重命名文件。
- Repoint any dependencies to the new file.
- 重新指向新文件的所有依赖项。
- If the renamed migration was already applied, apply it again using
--fake
. - 如果已应用重命名的迁移,请使用--fake再次应用它。
If it's a brand new migration, 2 and 3 won't apply, and it's perfectly fine to rename them.
如果它是一个全新的迁移,2和3将不适用,并且重命名它们是完全正常的。
#2
11
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.
每次迁移被压扁时,都会在Django中发生这种情况。生成包含类变量替换的新文件,该文件列出了要替换的迁移文件。
So to rename a file migration file add in the following variable in the Migration class:
因此,要重命名文件迁移文件,请在Migration类中添加以下变量:
replaces = [('app name', 'migration file name'), ]
And everything works like it did before the file change.
一切都像文件更改之前一样。