gpt4 book ai didi

Django Migration 未应用迁移更改

转载 作者:行者123 更新时间:2023-12-04 00:02:07 25 4
gpt4 key购买 nike

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

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 命令说没有要应用的迁移。

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

最佳答案

删除迁移目录绝不是一个好主意,因为 Django 会忘记哪些迁移已经应用,哪些没有应用(一旦应用程序部署到某个地方,就很难让事情恢复同步)。

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



要恢复,您可以尝试重置模型 在添加/删除字段 之前与它们完全匹配.然后你可以运行
$ python manage.py makemigrations myproj

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

使用 Django 1.7:
$ python manage.py migrate myproj

使用 Django >= 1.8:
$ python manage.py migrate myproj --fake-initial

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

关于Django Migration 未应用迁移更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29902366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com