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
IurNusRay
V2EX  ›  Python

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

  •  
  •   IurNusRay · 2021-12-14 16:35:12 +08:00 · 1710 次点击
    这是一个创建于 872 天前的主题,其中的信息可能已经有所发展或是发生改变。
    with engine.begin() as connection:
    正常执行
    connection.execute(first_sql)

    此句报异常
    connection.execute(second_sql)


    如上的代码,second_sql 故意设置了 sql 语法错误,但是 first_sql 执行的修改没有回滚,是哪里用的不对吗
    第 1 条附言  ·  2021-12-15 09:29:04 +08:00
    原来是 Mysql 不支持表 alter, truncate 的回滚啊
    5 条回复    2021-12-15 13:56:08 +08:00
    seven123
        1
    seven123  
       2021-12-14 17:10:30 +08:00
    需要 connection.execute("rollback;")
    IurNusRay
        2
    IurNusRay  
    OP
       2021-12-14 17:36:01 +08:00
    @seven123 也是无效的,刚刚好像发现原因了,我是执行的表结构修改的 sql ,如果是执行数据插入 /删除之类的 sql ,是会正常回滚的
    abersheeran
        3
    abersheeran  
       2021-12-15 09:58:54 +08:00
    哈哈哈感谢,我又多一个理由不支持 MySQL 了。PostgreSQL 支持表修改的回滚
    0x0208v0
        4
    0x0208v0  
       2021-12-15 13:36:38 +08:00
    我说的呢,pg alter 表的时候需要 session.commit()才能成功,mysql 直接成功都不需要 commit ,刚开始还以为是自己脚本写错了,原来是这么回事,感谢楼主,学到了
    todd7zhang
        5
    todd7zhang  
       2021-12-15 13:56:08 +08:00
    看样子是我狭隘了,一直没了解过这个。这种情况下居然不支持回滚...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   770 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:02 · PVG 06:02 · LAX 15:02 · JFK 18:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.