kalombo
kalombo

Reputation: 871

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?

Upvotes: 1

Views: 2005

Answers (2)

kalombo
kalombo

Reputation: 871

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

Upvotes: 4

univerio
univerio

Reputation: 20518

You'll want to do:

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

Upvotes: 2

Related Questions