使用 Django South 向后迁移

发布于 2024-11-03 22:25:56 字数 300 浏览 1 评论 0原文

好吧,这似乎是一个非常愚蠢的问题,而且我确信我在某个地方遗漏了一些东西。

如何在 Django 上使用 South 执行向后迁移?

因此,我调整了我的模型,使用 schemamigration 创建了迁移,使用 migrate 运行迁移,现在我意识到这并不完全是我想要的,我希望它恢复正常很久以前。

如果没有手动编辑数据库表和删除迁移文件,我应该如何回滚迁移?我通过 Google 找到了使用 South 进行向后迁移的参考,但尚未找到可靠的代码示例。

有人可以帮忙吗?

Ok, so this seems like a really silly thing to ask, and I'm sure I'm missing something somewhere.

How do you perform a backwards migration using South on Django?

So I've tweaked my models, created a migration with schemamigration, run the migration with migrate, and now I realise that's not quite what I wanted and I want it back the way before.

Short of manually editing db tables and removing migration files, how should I go about rolling the migration back? I find references to backward migrations using South via Google, but have yet to find a solid code example for it.

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-11-10 22:25:56

您需要计算出您想要回滚的迁移之前的迁移编号。

您的应用程序应该有一个迁移目录,其中的文件命名为

0000_initial.py
0001_added_some_fields.py
0002_added_some_more_fields.py
0003_deleted_some_stuff.py

通常,当您运行 ./manage.py migrate your_app 时,South 按顺序运行所有新迁移。 (它查看数据库表来决定哪些是“新的”)。

不过,您也可以按编号指定任何迁移,South 将向前或向后迁移您的数据库,以将其迁移到该点。因此,对于上面的示例文件,如果您已经迁移到 0003,并且您想要反向运行 0003(有效地撤消它),您将运行

./manage.py migrate your_app 0002

South 来查看数据库,意识到它已经运行了 0003,并确定必须对0003运行反向迁移才能回到0002。

You need to figure out the number of the migration just before the one you want to roll back.

Your app should have a migrations directory, with files in it named like

0000_initial.py
0001_added_some_fields.py
0002_added_some_more_fields.py
0003_deleted_some_stuff.py

Normally, when you run ./manage.py migrate your_app, South runs all new migrations, in order. (It looks at the database tables to decide which ones are 'new').

However, you can also specify any migration by number, and South will migrate your database, either forward or backward, to take it to that point. So, with the example files above, if you have already migrated up to 0003, and you wanted to run 0003 in reverse (undoing it, effectively), you would run

./manage.py migrate your_app 0002

South would look at the database, realise that it has run 0003 already, and determine that it has to run the reverse migration for 0003 in order to get back to 0002.

﹏半生如梦愿梦如真 2024-11-10 22:25:56

以防万一有人(比如我)想知道如何从初始 (0001) 迁移回来

django-admin.py migrate some_app zero

输出:

Running migrations for some_app:
 - Migrating backwards to zero state.
 < some_app:0001_initial

“零”是任何迁移之前的特殊状态。

参考:http://south.aeracode.org/docs/commands.html

Just in case someone (like me) wondered how to migrate back from initial (0001):

django-admin.py migrate some_app zero

output:

Running migrations for some_app:
 - Migrating backwards to zero state.
 < some_app:0001_initial

"zero" is a special state before any migration.

Reference: http://south.aeracode.org/docs/commands.html

装迷糊 2024-11-10 22:25:56

在参数末尾添加迁移名称:

./manage.py migrate app-name 00xx-migration-name

Add a migration name at the end of the parameters:

./manage.py migrate app-name 00xx-migration-name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文