V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
css3
V2EX  ›  Python

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

  •  
  •   css3 · 2019-10-29 15:38:36 +08:00 · 3969 次点击
    这是一个创建于 1612 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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有变化一样,有什么解决办法吗,很头疼的问题😭

    第 1 条附言  ·  2019-10-29 16:20:30 +08:00
    各种招都试了
    1、删同步记录
    2、删了 django_migrations 表中对应的 app_name 同步记录
    3、同步前,先 fake
    4、更新后再同步

    以上方法均无效
    由于项目着急,用了最不想用的一招,直接删库后同步过来了,但解决的方法还是没有😭
    21 条回复    2019-11-01 11:17:15 +08:00
    xujunfu
        1
    xujunfu  
       2019-10-29 15:48:59 +08:00
    文档写了要加 app_name
    python manage.py makemigrations app_name
    python manage.py migrate
    css3
        2
    css3  
    OP
       2019-10-29 15:57:49 +08:00
    @xujunfu 加上 app_name 也是一样的同步失效😭
    css3
        3
    css3  
    OP
       2019-10-29 16:01:11 +08:00
    @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
        4
    Jammar  
       2019-10-29 16:07:22 +08:00
    删掉 migrations 重新迁移
    Hopetree
        5
    Hopetree  
       2019-10-29 16:09:26 +08:00
    把迁移的文件都删掉,之保留__init__.py ,然后重新生成
    css3
        6
    css3  
    OP
       2019-10-29 16:13:27 +08:00
    @Jammar
    @Hopetree 试了无效😭
    libaibuaidufu
        7
    libaibuaidufu  
       2019-10-29 16:15:25 +08:00
    没有在 settings 中 添加对应模块?
    careofzm
        8
    careofzm  
       2019-10-29 16:15:57 +08:00
    去数据库里的 django_migrate 表看一下, 看是不是已经生成了, 这个表是记录执行迁移操作的
    est
        9
    est  
       2019-10-29 16:19:44 +08:00
    多 db 需要单独制定迁移哪一个 db。
    hmxxmh
        10
    hmxxmh  
       2019-10-29 16:20:05 +08:00
    是否是你修改了某个表,新增了字段,但是没给默认值,这样迁移会失败。
    ksedz
        11
    ksedz  
       2019-10-29 16:29:01 +08:00
    managed 是 False ?或者自定义 database router 里面逻辑不对?
    lzj307077687
        12
    lzj307077687  
       2019-10-29 16:29:57 +08:00
    django_migrations 表清掉?
    css3
        13
    css3  
    OP
       2019-10-29 16:34:42 +08:00
    css3
        14
    css3  
    OP
       2019-10-29 16:40:56 +08:00
    @careofzm 执行前有,执行后没生成新的
    libaibuaidufu
        15
    libaibuaidufu  
       2019-10-29 17:48:05 +08:00
    我感觉有时候会莫名的无法检测到 model 变化,一般都是重来。。。 没仔细深究过。。。
    css3
        16
    css3  
    OP
       2019-10-29 17:50:43 +08:00
    @libaibuaidufu +10000😂不知道原因,只能删库重来
    laoyur
        17
    laoyur  
       2019-10-29 17:51:32 +08:00
    managed 一开始是 False,后来又改回 True 了吧?再改回 True 后,它会跳过你那个 Model 类的
    shanguo
        18
    shanguo  
       2019-10-29 18:10:20 +08:00
    在 models 包里面的 __init__.py 里面导入 你的 TestModel

    举例:
    models/
    __init__.py
    user.py


    __init__.py 里面 导入你的 UserModel

    然后在执行 makemigrations,migrate
    wuwukai007
        19
    wuwukai007  
       2019-10-30 18:57:54 +08:00
    你是不是用了自定义用户类,AbstractUser,然后又改了,如果这样要到 django 源码里面删 makemigrations 文件,有缓存的
    css3
        20
    css3  
    OP
       2019-11-01 11:17:02 +08:00 via iPhone
    @shanguo 需要这么操作吗?是个空文件啊 init
    css3
        21
    css3  
    OP
       2019-11-01 11:17:15 +08:00 via iPhone
    @wuwukai007 没这样操作啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3104 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:58 · PVG 20:58 · LAX 05:58 · JFK 08:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.