根据某些 DBMS 的这个问题,可以回滚 CREATE TABLE 语句。特别是 sqlite 是可能的(尽管它没有记录)。
所以我的问题是,是否可以在 sqlalchemy 中回滚 create_all ?我试图编写一些测试代码,但它似乎不起作用:
>>> engine = create_engine('sqlite:///:memory:')
>>> engine
Engine(sqlite:///:memory:)
>>> Session = sessionmaker(bind=engine)
>>> connection = engine.connect()
>>> session = Session(bind=connection)
>>> engine.table_names()
[]
>>> transaction = connection.begin()
>>> Base = declarative_base()
>>> class Test(Base):
... __tablename__ = 'TEST'
... id = Column(Integer, primary_key=True)
...
>>> Base.metadata.bind = engine
>>> Base.metadata.create_all()
>>> engine.table_names()
[u'TEST']
>>> transaction.rollback()
>>> session.close()
>>> connection.close()
>>> engine.table_names()
[u'TEST']