How can I skip a migration with Django migrate command?

 ̄綄美尐妖づ 提交于 2019-12-04 16:52:28

问题


First, I am asking about Django migration introduced in 1.7, not south.

Suppose I have migrations 001_add_field_x, 002_add_field_y, and both of them are applied to database. Now I change my mind and decide to revert the second migration and replace it with another migration 003_add_field_z.

In other words, I want to apply 001 and 003, skipping 002, how can I do this?

P.S. I know I can migrate backwards to 001, but after I make the 003 migration and execute migrate command, 001 through 003 will all be applied, right?


回答1:


You can use the --fake option.

Once you revert to 0001 you can run

python manage.py migrate <app> 0002 --fake

and then run

python manage.py migrate <app> #Optionally specify 0003 explicitly

which would apply only 0003 in this case.

If you do not want to follow this process for all the environment/other developers, you can just remove the migration files, and run a new makemigration, and commit that file - and yes, do run migrate with the --fake option




回答2:


Not applicable to this specific case, but if one wants or needs to skip all unapplied migrations, this can be used:

python manage.py migrate --fake

Just saves a bit of typing.



来源:https://stackoverflow.com/questions/31369466/how-can-i-skip-a-migration-with-django-migrate-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!