如何使用sqlalchemy事务回滚创建表? (源码)

时间:2017-08-14 15:40:39

标签: python sqlalchemy

#!/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

我有这个python脚本。我的期望是,数据库之后没有表测试,但确实如此。我做错了什么/如何回滚create table

$ python3 --version
Python 3.5.3

0 个答案:

没有答案