How to rollback create table with sqlalchemy transaction? (sqlite)

Home / Uncategorized / How to rollback create table with sqlalchemy transaction? (sqlite)

Question:
#!/usr/bin/env python3
import logging
import sqlalchemy

logging.basicConfig()
logging.getLogger(‘sqlalchemy’).setLevel(logging.DEBUG)
engine = sqlalchemy.create_engine("sqlite:///test.db")
connection = engine.connect()
transaction = connection.begin()
try:connection.execution_options(autocommit=False).execute("create table test (`id` int NOT NULL);")
connection.execution_options(autocommit=False).execute("create table test (`id` int NOT NULL);")
transaction.commit()
except:transaction.rollback()
raise
I have this python script. My expectation would be, that database doesn’t have table test afterwards, but it does. What am I doing wrong/How can I rollback create table?


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *