如何在事务中包装 create 和 sqlalchemy-migrate alter 语句
我正在尝试使用 sqlalchemy table.create 和 sqlalchemy-migrate table.rename 方法对我的数据库进行一些数据库架构更改,以及一些插入到 select 语句中的操作。我想将所有这些都包含在一个事务中。我不知道该怎么做。这就是我尝试过的:
engine = sqlalchemy.engine_from_config(conf.local_conf, 'sqlalchemy.')
trans = engine.connect().begin()
try:
old_metatadata.tables['address'].rename('address_migrate_tmp', connection=trans)
new_metatadata.tables['address'].create(connection=trans)
except:
trans.rollback()
raise
else:
trans.commit()
但它出现错误:(
AttributeError: 'RootTransaction' object has no attribute '_run_visitor'
我尝试使用 sqlalchemy-migrate column.alter(name='newname') 但出现错误,并且在事务中不起作用,因此使我的数据库处于损坏状态。我还需要重命名多个列,因此我决定推出自己的代码。)
I'm trying some db schema changes to my db, using the sqlalchemy table.create and sqlalchemy-migrate table.rename methods, plus some insert into select statments. I want to wrap all of this in a transaction. I can't figure out how to do this. This is what I tried:
engine = sqlalchemy.engine_from_config(conf.local_conf, 'sqlalchemy.')
trans = engine.connect().begin()
try:
old_metatadata.tables['address'].rename('address_migrate_tmp', connection=trans)
new_metatadata.tables['address'].create(connection=trans)
except:
trans.rollback()
raise
else:
trans.commit()
But it errors with:
AttributeError: 'RootTransaction' object has no attribute '_run_visitor'
(I tried using sqlalchemy-migrate column.alter(name='newname') but that errors, and does not work in a transaction, and so leaves my db in a broken state. I also need to rename multiple columns, and so I decide to roll my own code.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊 - 我需要简单地使用创建事务的连接。
Ah - I need to simply use the connection that the transaction was created on.