使用 Django South 向后迁移
好吧,这似乎是一个非常愚蠢的问题,而且我确信我在某个地方遗漏了一些东西。
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要计算出您想要回滚的迁移之前的迁移编号。
您的应用程序应该有一个迁移目录,其中的文件命名为
通常,当您运行
./manage.py migrate your_app
时,South 按顺序运行所有新迁移。 (它查看数据库表来决定哪些是“新的”)。不过,您也可以按编号指定任何迁移,South 将向前或向后迁移您的数据库,以将其迁移到该点。因此,对于上面的示例文件,如果您已经迁移到 0003,并且您想要反向运行 0003(有效地撤消它),您将运行
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
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
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.
以防万一有人(比如我)想知道如何从初始 (0001) 迁移回来:
输出:
“零”是任何迁移之前的特殊状态。
参考:http://south.aeracode.org/docs/commands.html
Just in case someone (like me) wondered how to migrate back from initial (0001):
output:
"zero" is a special state before any migration.
Reference: http://south.aeracode.org/docs/commands.html
在参数末尾添加迁移名称:
Add a migration name at the end of the parameters: