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

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

问题描述

好的,所以这个问题似乎是一个很傻的事情,我确定我在某处找不到某些东西。



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



所以我调整了我的模型,创建了一个使用 schemamigration 的迁移,运行迁移与迁移,现在我意识到这不是我想要的,我想要的方式。



手动编辑数据库表和删除迁移文件不足,我应该如何回滚迁移?我发现使用Google的向后迁移的引用,但尚未找到一个坚实的代码示例。



任何人都可以帮助?

解决方案

您需要确定在之前迁移的数量



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

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

通常,当您运行 ./ manage.py migrate your_app 时,南依次运行所有新的迁移。 (它查看数据库表以确定哪些是新)。



但是,您还可以按数字指定任何迁移,而南方将迁移您的数据库, 向前或向后,以便将其转换到这一点。所以,使用上面的示例文件,如果您已经迁移到0003,并且要反过来运行0003(有效地撤销它),则您将运行

  ./ manage.py migrate your_app 0002 

南会看数据库,意识到它已经运行0003,并确定它必须运行0003的反向迁移,以回到0002。


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?

解决方案

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.

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

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