简体   繁体   English

如何避免在flask-sqlalchemy中的Raw SQL中使用ROLLBACK语句

[英]How to avoid ROLLBACK statement in Raw SQL in flask-sqlalchemy

I am trying to send NOTIFY in postgresql through sqlalchemy. 我正在尝试通过sqlalchemy在PostgreSQL中发送NOTIFY。 Here is the part of code: 这是代码的一部分:

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
db.engine.execute("NOTIFY DHCP")

Which generates the following SQL code: 生成以下SQL代码:

2016-11-29 14:58:41 +05 [20571-16] postgres@server LOG:  statement: BEGIN
2016-11-29 14:58:41 +05 [20571-17] postgres@server LOG:  statement: NOTIFY DHCP
2016-11-29 14:58:41 +05 [20571-18] postgres@server LOG:  statement: ROLLBACK

Why do i have ROLLBACK statement in the code and how to change to COMMIT one? 为什么我在代码中有ROLLBACK语句,以及如何更改为COMMIT one?

You'll want to do: 您需要执行以下操作:

db.session.execute("...")
db.session.commit()

Here is the best solution that i could find: 这是我能找到的最佳解决方案:

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
connection = db.engine.connect()
transaction = connection.begin()
try:
    connection.execute("NOTIFY DHCP")
    transaction.commit()
except:
    transaction.rollback()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM