Django迁移不适用迁移更改 [英] Django Migration is not applying the migration changes

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

问题描述

使用django 1.7.7我想使用django的迁移来添加或删除一个字段。
所以我修改了model.py并运行

Using django 1.7.7 I want to use django's migration to add or remove a field. so I modified model.py and ran

python manage.py makemigrations myproj
Migrations for 'myproj':
  0001_initial.py:
    - Create model Interp
    - Create model InterpVersion

python manage.py migrate myproj
Operations to perform:
  Apply all migrations: myproj
Running migrations:
  Applying myproj.0001_initial... FAKED

python manage.py runserver

然后检查管理页面,它没有更新。
然后我尝试删除迁移文件夹,再次尝试; migrate命令表示没有可以应用的迁移。

Then checked the admin page, it is not updated. Then I tried removing the migration folder and tried again; the migrate command says there are no migrations to apply.

如何进行迁移?
注意:我想使用新技术,使用django的迁移,而不是旧的南方法。

How can I do the migration? Note: I want to use the new technique using django's migration not the old south approach.

推荐答案

删除迁移目录永远不是一个好主意,因为Django然后失去了哪些迁移已经被应用的跟踪,哪些不是(并且一旦应用程序被部署到某处,这将变得相当困难,使事情恢复同步)。

Deleting the migration directory is never a good idea, because Django then loses track of which migration has been applied and which not (and once an app is deployed somewhere it can become quite difficult to get things back in sync).


免责声明:每当出现这种情况发生时,最好备份数据库,如果它包含任何有价值的东西。如果在早期发展中没有必要,但是一旦后台的事情不同步,就有机会越来越糟糕。 : - )

Disclaimer: whenever things like that occur it is best to backup the database if it contains anything valuable. If in early development it is not necessary, but once things on the backend get out of sync there is a chance of things getting worse. :-)






要恢复,您可以尝试重新设置模型 em>以匹配您添加/删除字段之前的内容 。然后你可以运行


To recover, you could try resetting your models to match exactly what they were before you have added/removed the fields. Then you can run

$ python manage.py makemigrations myproj

这将导致初始迁移( 0001_initial ... )。那么你可以告诉Django伪造这个迁移,这意味着告诉它将它的内部计数器设置为这个 0001_initial

which will lead to an initial migration (0001_initial...). Then you can tell Django to fake that migration, which means to tell it to set its internal counter to this 0001_initial:

与Django 1.7:

With Django 1.7:

$ python manage.py migrate myproj

使用Django> = 1.8:

With Django >= 1.8:

$ python manage.py migrate myproj --fake-initial

现在,尝试更改模型并运行 makemigrations 再次。现在应该创建一个可以按预期运行的 0002_foobar 迁移。

Now, try to change your model and run makemigrations again. It should now create a 0002_foobar migration that you could run as expected.

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

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