Python 有没有办法在 Django 上撤消迁移并从 showmigrations 列表中取消选中它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43267339/
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 22:44:44  来源:igfitidea点击:

There's a way to undo a migration on Django and uncheck it from the list of showmigrations?

pythondjangomigration

提问by César Villase?or

Actually, what I do is:

其实我的做法是:

  • Delete the migration file.
  • Delete the row from the table of django_migrations on the database.
  • Delete the changes applied by the migration that I want to delete or unapplied.
  • 删除迁移文件。
  • 从数据库的 django_migrations 表中删除该行。
  • 删除我要删除或未应用的迁移所应用的更改。

I want to know if there's another way to do this.

我想知道是否有另一种方法可以做到这一点。

回答by Astik Anand

You can revert back by migrating to the previous migration. See the migrations folder of your app and then see all the migrations

您可以通过迁移到之前的迁移来恢复。查看应用程序的迁移文件夹,然后查看所有迁移

Say for an example, if your migrations are something like below ordered number wise and latest migration 0012_latest_migration is applied currently.

举个例子,如果你的迁移类似于下面的有序数字,并且当前应用了最新的迁移 0012_latest_migration。

0010_previous_migration
0011_next_migration
0012_latest_migration

And You want to go back to 0010_previous_migration

你想回到 0010_previous_migration

./manage.py migrate my_app 0010_previous_migration 

and then you can delete all migrations after that like here delete both 0011_next_migration and 0012_latest_migration as you already applied 0010_previous_migration.

然后您可以删除所有迁移,就像这里删除 0011_next_migration 和 0012_latest_migration 一样,因为您已经应用了 0010_previous_migration。

If you're using Django 1.8+, you can show the names of all the migrations with

如果您使用的是 Django 1.8+,则可以显示所有迁移的名称

./manage.py showmigrations my_app

To reverse all migrations for an app to initial or start, you can run:

要将应用程序的所有迁移反转为初始或启动,您可以运行:

./manage.py migrate my_app zero