Python 如何使用 Django migrate 命令跳过迁移?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31369466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:52:41  来源:igfitidea点击:

How can I skip a migration with Django migrate command?

pythondjangodjango-modelsdjango-migrations

提问by NeoWang

First, I am asking about Django migration introduced in 1.7, not south.

首先,我问的是 1.7 中引入的 Django 迁移,而不是south.

Suppose I have migrations 001_add_field_x, 002_add_field_y, and both of them are applied to database. Now I change my mind and decide to revert the second migration and replace it with another migration 003_add_field_z.

假设我有 migrations 001_add_field_x002_add_field_y,并且它们都应用于数据库。现在我改变主意并决定恢复第二次迁移并将其替换为另一个迁移003_add_field_z

In other words, I want to apply 001 and 003, skipping 002, how can I do this?

换句话说,我想申请001和003,跳过002,我该怎么做?

P.S. I know I can migrate backwards to 001, but after I make the 003 migration and execute migrate command, 001 through 003 will all be applied, right?

PS我知道我可以向后迁移到001,但是在我进行003迁移并执行迁移命令后,001到003都会被应用,对吧?

采纳答案by karthikr

You can use the --fakeoption.

您可以使用该--fake选项。

Once you revert to 0001you can run

一旦你恢复到0001你可以运行

python manage.py migrate <app> 0002 --fake

and then run

然后运行

python manage.py migrate <app> #Optionally specify 0003 explicitly

which would apply only 0003in this case.

这仅适用0003于这种情况。

If you do not want to follow this process for all the environment/other developers, you can just remove the migration files, and run a new makemigration, and commit that file - and yes, do run migratewith the --fakeoption

如果您不想为所有环境/其他开发人员遵循此过程,您可以删除迁移文件,然后运行一个新的makemigration,并提交该文件 - 是的,请migrate使用--fake选项运行

回答by Nagev

Not applicable to this specific case, but if one wants or needs to skip allunapplied migrations, this can be used:

不适用于这种特定情况,但如果想要或需要跳过所有未应用的迁移,可以使用:

python manage.py migrate --fake

Just saves a bit of typing.

只是节省了一点打字的时间。