2016-11-29 63 views
0

我想通過sqlalchemy在postgresql中發送NOTIFY。這裏是代碼的一部分:如何在flask-sqlalchemy中的Raw SQL中避免ROLLBACK語句

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

產生下面的SQL代碼:

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

爲什麼我必須ROLLBACK語句中的代碼和如何更改承諾嗎?

回答

0

這裏是我能找到的最好的解決辦法:

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

你想做的事:

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