Closed victorterpstra closed 2 years ago
To use a session and execute RAW SQL, see: https://stackoverflow.com/questions/17972020/how-to-execute-raw-sql-in-flask-sqlalchemy-app
Combining the 2 references (?):
with Session(engine) as session, session.begin():
session.execute(some_sql)
session.execute(some_other_sql)
Added support for Transactions in v0.5.3.0b Based on https://docs.sqlalchemy.org/en/14/core/connections.html (i.e. NOT using Session)
Basic usage in code:
with self.engine.begin() as connection:
connection.execute(sql)
Unfortunately, using transactions does NOT avoid FK constraint violations when:
The effects are different in SQLite vs DB2:
There is a flag 'deferrable' in ForeignKeyConstraint that is supposed to tell the DB to defer FK constraint checking to after the transaction is complete. But that doesn't seem to work in SQLite. Worse in DB2: this flag causes an exception creating the schema.
Validated in v0.5.3.0b that FK violations (or any other exception) will cause a rollback for replace_scenario_in_db The following APIs on ScenarioDbManager support transactions (and rollbacks):
Delete all tables within one session/cursor, so we don't have to worry about the order of the delete?
See: https://docs.sqlalchemy.org/en/14/orm/session.html