推荐学习书目
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
kadycui
V2EX  ›  Python

django 使用 orm 修改某个数据库字段值时, save()后大约过了三分钟,这个字段的值又回到了修改之前的值. 不是每次都必现的

  •  
  •   kadycui · May 12, 2020 · 3562 views
    This topic created in 2197 days ago, the information mentioned may be changed or developed.

    save()之后代码没有报错,数据库字段已经更改成功,之后 3-5 分钟字段又恢复到原来的状态. 这段代码是每天都执行的自动脚本的一小部分 这个字段每天都会被更改

                    if ser_list:
                        for s in ser_list:
                            create_time = s.create_time
                            dt = now - create_time
                            if dt.days == 0 and dt.seconds / 60 > 0 and dt.seconds / 60 < 3:
                                s.status = 4
                                s.commend = 1
                                s.last_time = datetime.datetime.now()
                                s.save()
    

    业务环境(django 1.6+python2.6+mysql5.6 ),祖传项目项目,有点老

    10 replies    2020-05-16 12:24:41 +08:00
    37Y37
        1
    37Y37  
       May 12, 2020
    可能数据问题,也可能是有别的脚本改了数据
    encro
        2
    encro  
       May 12, 2020
    log
    ipwx
        3
    ipwx  
       May 12, 2020
    可能是别的什么地方读出来了又写回去了。好像 Django 的 .save() 是把所有字段重新存一遍,而不是只更新 dirty fields 。
    cominghome
        4
    cominghome  
       May 12, 2020
    找 audit 或者 bin log,看你的描述不觉得是 orm 的问题
    wzwwzw
        5
    wzwwzw  
       May 13, 2020
    save 是把整个对象都重新存一遍,所以检查下 log 是不是有别的地方也更新过。
    chaohuang
        6
    chaohuang  
       May 13, 2020
    用 update 吧
    kadycui
        7
    kadycui  
    OP
       May 13, 2020
    @ipwx 现在排查出来有一个地方也有 save()操作
    kadycui
        8
    kadycui  
    OP
       May 13, 2020
    @wzwwzw 是还有一个地方也存在 save(),现在测试下数据
    codeli
        9
    codeli  
       May 13, 2020
    查日志
    wobushibaoabao1
        10
    wobushibaoabao1  
       May 16, 2020
    这个问题实际上是因为,另外还有一个定时任务工作中, 使用到了同样的数据库模型, 同时,也会有 save 操作, 在偶然的时间点里, 这个脚本拿到了缓存的 query_set, 然后进行了 save 动作,导致,另一个定时任务的某个字段修改失败.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1406 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 17:07 · PVG 01:07 · LAX 10:07 · JFK 13:07
    ♥ Do have faith in what you're doing.