使用 Django South 向后迁移 [英] Backwards migration with Django South

查看:20
本文介绍了使用 Django South 向后迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以这似乎是一个非常愚蠢的问题,我确定我在某个地方遗漏了一些东西.

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

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

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

所以我调整了我的模型,使用 schemamigration 创建了迁移,使用 migrate 运行迁移,现在我意识到这不是我想要的,我想要的它回到了以前的方式.

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.

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

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.

谁能帮忙?

推荐答案

你需要弄清楚迁移的数量就在你想回滚的那个之前.

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

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

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').

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

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 会查看数据库,发现它已经运行了 0003,并确定它必须对 0003 运行反向迁移才能返回到 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.

这篇关于使用 Django South 向后迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆