简体   繁体   English

Flask-SQLAlchemy session.rollback不起作用

[英]Flask-SQLAlchemy session.rollback doesnt work

I´m developing an API with FLASK and I´m using FLASK-SQALCHEMY. 我正在使用FLASK开发API,并且正在使用FLASK-SQALCHEMY。

Well, when the api throws some exception, I try to rollback the session. 好吧,当api引发某些异常时,我尝试回滚会话。

My Code 我的密码

def create_user():
    user = User()
    db.session.add(user)
    db.session.flush()
    return user

try:
    new_user = create_user()
    print('x' + 2) # force error
    db.session.commit()
except Exception as error:
    db.session.rollback()
    raise

My requirements (Python 3.6.3 & MySQL 5.7): 我的要求(Python 3.6.3和MySQL 5.7):

SQLAlchemy==1.2.2
astroid==1.6.0
flask==0.12.2
flask-login==0.4.1
Flask-Migrate==2.1.1
flask-script==2.0.6
flask-sqlalchemy==2.3.2
flask-validator==1.2.3
isort==4.2.15
pygelf==0.3.3
pylint==1.8.1
PyMySQL==0.7.11
pytest==3.3.1
Werkzeug==0.13

From the docs : 文档

The FLUSH statement causes an implicit commit FLUSH语句导致隐式提交

Remove the db.session.flush() and it should work as expected. 删除db.session.flush() ,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用SQLAlchemy的Flask应用程序中,可以将“ session.rollback”永久放在应用程序的开头吗? - In a Flask application that is using SQLAlchemy, is it ok to permanently put `session.rollback` at the beginning of the app? 如果引发异常,如何使 Flask-SQLAlchemy 自动回滚会话? - How to make Flask-SQLAlchemy automatically rollback the session if an exception is raised? 使用session.add或session.execute进行Flask-SQLAlchemy事务回滚的不同行为 - Different behaviour of Flask-SQLAlchemy transaction rollback using session.add or session.execute IntegrityError的Sqlalchemy session.rollback会导致queuepool用完处理程序吗? - Sqlalchemy session.rollback from IntegrityError causes queuepool to run out of handlers? pytest Flask-SQLAlchemy 会话中的完整性错误 - Integrity error in pytest Flask-SQLAlchemy session Flask-sqlalchemy禁用整个会话的autoflush - Flask-sqlalchemy disable autoflush for the whole session 如何避免在flask-sqlalchemy中的Raw SQL中使用ROLLBACK语句 - How to avoid ROLLBACK statement in Raw SQL in flask-sqlalchemy 保存到Flask-SQLAlchemy会话之外的数据库 - Save to database outside of Flask-SQLAlchemy session 我如何更好地处理Flask-SQLAlchemy提交/回滚? - How can I better handle this Flask-SQLAlchemy commit/rollback? Flask-SQLAlchemy和SQLAlchemy - Flask-SQLAlchemy and SQLAlchemy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM