django 同步数据库失效怎么解决

2019 年 10 月 29 日
 css3

django Version: 2.2.3 更新了models.py后,执行

root@:collection:/code# python manage.py makemigrations
No changes detected

root@:collection:/code# python manage.py migrate
Operations to perform:
  Apply all migrations: account, admin, auth, authtoken, sessions, sites, socialaccount, template
Running migrations:
  No migrations to apply.

看上去是检测不到models.py有变化一样,有什么解决办法吗,很头疼的问题😭

5232 次点击
所在节点    Python
21 条回复
xujunfu
2019 年 10 月 29 日
文档写了要加 app_name
python manage.py makemigrations app_name
python manage.py migrate
css3
2019 年 10 月 29 日
@xujunfu 加上 app_name 也是一样的同步失效😭
css3
2019 年 10 月 29 日
@xujunfu
root@collection:/code# python manage.py makemigrations project
Migrations for 'project':
apps/project/migrations/0001_initial.py
- Create test Module
- Add field env to test
- Alter unique_together for module (1 constraint(s))
root@collection:/code# python manage.py migrate project
Operations to perform:
Apply all migrations: project
Running migrations:
No migrations to apply.
Jammar
2019 年 10 月 29 日
删掉 migrations 重新迁移
Hopetree
2019 年 10 月 29 日
把迁移的文件都删掉,之保留__init__.py ,然后重新生成
css3
2019 年 10 月 29 日
@Jammar
@Hopetree 试了无效😭
libaibuaidufu
2019 年 10 月 29 日
没有在 settings 中 添加对应模块?
careofzm
2019 年 10 月 29 日
去数据库里的 django_migrate 表看一下, 看是不是已经生成了, 这个表是记录执行迁移操作的
est
2019 年 10 月 29 日
多 db 需要单独制定迁移哪一个 db。
hmxxmh
2019 年 10 月 29 日
是否是你修改了某个表,新增了字段,但是没给默认值,这样迁移会失败。
ksedz
2019 年 10 月 29 日
managed 是 False ?或者自定义 database router 里面逻辑不对?
lzj307077687
2019 年 10 月 29 日
django_migrations 表清掉?
css3
2019 年 10 月 29 日
css3
2019 年 10 月 29 日
@careofzm 执行前有,执行后没生成新的
libaibuaidufu
2019 年 10 月 29 日
我感觉有时候会莫名的无法检测到 model 变化,一般都是重来。。。 没仔细深究过。。。
css3
2019 年 10 月 29 日
@libaibuaidufu +10000😂不知道原因,只能删库重来
laoyur
2019 年 10 月 29 日
managed 一开始是 False,后来又改回 True 了吧?再改回 True 后,它会跳过你那个 Model 类的
shanguo
2019 年 10 月 29 日
在 models 包里面的 __init__.py 里面导入 你的 TestModel

举例:
models/
__init__.py
user.py


__init__.py 里面 导入你的 UserModel

然后在执行 makemigrations,migrate
wuwukai007
2019 年 10 月 30 日
你是不是用了自定义用户类,AbstractUser,然后又改了,如果这样要到 django 源码里面删 makemigrations 文件,有缓存的
css3
2019 年 11 月 1 日
@shanguo 需要这么操作吗?是个空文件啊 init

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.ih06.com/t/614094

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX