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

sqlalchemy 事务中出错没有进行回滚?

  •  
  •   IurNusRay · Dec 14, 2021 · 2617 views
    This topic created in 1609 days ago, the information mentioned may be changed or developed.
    with engine.begin() as connection:
    正常执行
    connection.execute(first_sql)

    此句报异常
    connection.execute(second_sql)


    如上的代码,second_sql 故意设置了 sql 语法错误,但是 first_sql 执行的修改没有回滚,是哪里用的不对吗
    Supplement 1  ·  Dec 15, 2021
    原来是 Mysql 不支持表 alter, truncate 的回滚啊
    5 replies    2021-12-15 13:56:08 +08:00
    seven123
        1
    seven123  
       Dec 14, 2021
    需要 connection.execute("rollback;")
    IurNusRay
        2
    IurNusRay  
    OP
       Dec 14, 2021
    @seven123 也是无效的,刚刚好像发现原因了,我是执行的表结构修改的 sql ,如果是执行数据插入 /删除之类的 sql ,是会正常回滚的
    abersheeran
        3
    abersheeran  
       Dec 15, 2021
    哈哈哈感谢,我又多一个理由不支持 MySQL 了。PostgreSQL 支持表修改的回滚
    joApioVVx4M4X6Rf
        4
    joApioVVx4M4X6Rf  
       Dec 15, 2021
    我说的呢,pg alter 表的时候需要 session.commit()才能成功,mysql 直接成功都不需要 commit ,刚开始还以为是自己脚本写错了,原来是这么回事,感谢楼主,学到了
    todd7zhang
        5
    todd7zhang  
       Dec 15, 2021
    看样子是我狭隘了,一直没了解过这个。这种情况下居然不支持回滚...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   999 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    ♥ Do have faith in what you're doing.