【发布时间】:2016-09-06 13:07:09
【问题描述】:
我有一列 goods.visible(布尔值),我想用 goods.status(枚举)替换它。
我需要用另一列替换一列并使用 sqlalchemy 迁移数据(以使用其数据类型转换机制)。由于 sqlite 对此不支持 ALTER TABLE,因此我必须为此使用 batch_alter_table alembic 操作。
我的迁移如下所示。它抛出sqlite3.OperationalError: no such column: goods.status,因为在我的旧表中没有这样的列(新表是为使用批处理迁移而创建的)
def upgrade():
with op.batch_alter_table('goods') as batch_op:
batch_op.add_column(sa.Column('status', sa.Enum('published', 'unpublished', 'deleted'), nullable=True))
conn = op.get_bind()
Session = sa.orm.sessionmaker()
session = Session(bind=conn)
for good in session.query(Good):
# I want to perform some data updates & insert data into new column
batch_op.drop_column('visible')
如果我要添加 load_only 以指定要从表中选择的列:
sqlite3.OperationalError: no such column: goods.status
我会收到错误sqlalchemy.exc.ArgumentError: Can't find property named 'visible' on the mapped entity Mapper|Good|goods in this Query.。我明白我为什么得到它 - 我的模型中没有更多可见的属性。
在这种情况下如何使用 alembic 迁移数据?
我尝试使用原始查询.. 但它说没有表:
s = sa.sql.select([sa.sql.text('goods.*')])
for row in conn.execute(s):
print(row)
Hovewer 检查器显示 goods 表:
inspector = sa.inspect(conn.engine)
for table_name in inspector.get_table_names():
print(table_name)
for column in inspector.get_columns(table_name):
print("Column: %s" % column['name'])
展示
goods
Column: id
Column: name
Column: price
Column: visible
【问题讨论】:
-
尚不能解决您帖子的其余部分,但
sqlite3.OperationalError: no such column: goods.status的原因似乎是实际上with块内的批处理操作collects instructions registered throughbatch_op然后在__exit__执行他们。所以在 with 块中确实没有 status 列。
标签: python sqlalchemy