V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
kuzicala
V2EX  ›  Python

flask-sqlalchemy 默认是开始事务的,那么如何标记一组操作为一个 transaction 要么全成功要么全失败?

  •  
  •   kuzicala · Dec 16, 2019 · 3501 views
    This topic created in 2324 days ago, the information mentioned may be changed or developed.
    以下代码片段,提供的参数要求具有唯一性,所以最后一条肯定插入失败,但是前两条却成功了。
    想要的效果是,把这三个插入最为一个事务呢,要么全成功要么全失败
    try:
    db.session.add(Order("a1"))
    db.session.add(Order("a2"))
    db.session.add(Order("a2"))
    db.session.commit()
    except Exception as e:
    db.session.rollback()
    raise e
    4 replies    2019-12-23 11:11:41 +08:00
    Latin
        1
    Latin  
       Dec 17, 2019
    db.session.add(Order("a1"),Order("a2"),Order("a2")) ?
    kuzicala
        2
    kuzicala  
    OP
       Dec 17, 2019
    @Latin add 方法不支持多参数啊
    Latin
        3
    Latin  
       Dec 17, 2019
    @kuzicala add_all
    kayseen
        4
    kayseen  
       Dec 23, 2019
    测试了一下,就算你不用 add_all, 比如 a1 和 a2 正常, a3 有问题, 那么在你最后 commit 的时候, 前面的也并不会成功提交
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1802 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 16:24 · PVG 00:24 · LAX 09:24 · JFK 12:24
    ♥ Do have faith in what you're doing.