简体   繁体   中英

How to avoid ROLLBACK statement in Raw SQL in flask-sqlalchemy

I am trying to send NOTIFY in postgresql through sqlalchemy. Here is the part of code:

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

Which generates the following SQL code:

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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