修改数据作为 alembic 升级的一部分 [英] Modify data as part of an alembic upgrade

查看:22
本文介绍了修改数据作为 alembic 升级的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 alembic 升级的一部分,我想修改一些数据库数据.

I would like to modify some database data as part of an alembic upgrade.

我以为我可以在迁移升级中添加任何代码,但以下失败:

I thought I could just add any code in the upgrade of my migration, but the following fails:

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('smsdelivery', sa.Column('sms_message_part_id', sa.Integer(), sa.ForeignKey('smsmessagepart.id'), nullable=True))
    ### end Alembic commands ###

    from volunteer.models import DBSession, SmsDelivery, SmsMessagePart

    for sms_delivery in DBSession.query(SmsDelivery).all():
        message_part = DBSession.query(SmsMessagePart).filter(SmsMessagePart.message_id == sms_delivery.message_id).first()
        if message_part is not None:
            sms_delivery.sms_message_part = message_part

出现以下错误:

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SmsDelivery|smsdelivery, SQL expression or this Session

我不太理解这个错误.我该如何解决这个问题,或者不可能进行这样的操作?

I am not really understanding this error. How can I fix this or is doing operations like this not a possibility?

推荐答案

从您提供的代码摘录中,很难理解您究竟想要实现什么.但我会试着猜测.所以下面的答案将基于我的猜测.

It is difficult to understand what exactly you are trying to achieve from the code excerpt your provided. But I'll try to guess. So the following answer will be based on my guess.

第 4 行 - 您从模块中导入内容(DBSession、SmsDelivery、SmsMessagePart),然后尝试像在应用程序中一样操作这些对象.

Line 4 - you import things (DBSession, SmsDelivery, SmsMessagePart) form your modules and then you are trying to operate with these objects like you do in your application.

错误表明 SmsDelivery 是一个映射器对象 - 因此它指向某个表.映射器对象应该绑定到有效的 sqlalchemy 连接.

The error shows that SmsDelivery is a mapper object - so it is pointing to some table. mapper objects should bind to valid sqlalchemy connection.

这告诉我您跳过了数据库对象的初始化(连接并将此连接绑定到映射器对象),就像您在应用程序代码中通常所做的那样.

Which tells me that you skipped initialization of DB objects (connection and binding this connection to mapper objects) like you normally do in your application code.

DBSession 看起来像 SQLAlchemy 会话对象 - 它也应该有连接绑定.

DBSession looks like SQLAlchemy session object - it should have connection bind too.

Alembic 已经准备好连接并打开 - 用于更改您使用 op.* 方法请求的数据库架构.

Alembic already has connection ready and open - for making changes to db schema you are requesting with op.* methods.

所以应该有办法获得这个连接.

So there should be way to get this connection.

根据 Alembic 手册 op.get_bind() 将返回当前连接绑定:
要与连接的数据库进行完全交互,请使用上下文中可用的绑定":

According to Alembic manual op.get_bind() will return current Connection bind:
For full interaction with a connected database, use the "bind" available from the context:

from alembic import op
connection = op.get_bind()

因此您可以使用此连接将查询运行到数据库中.

So you may use this connection to run your queries into db.

附注.我假设您想对表中的数据进行一些修改.您可以尝试将此修改表述为一个更新查询.Alembic 具有执行此类更改的特殊方法 - 因此您无需处理连接.
alembic.operations.Operations.execute

PS. I would assume you wanted to perform some modifications to data in your table. You may try to formulate this modification into one update query. Alembic has special method for executing such changes - so you would not need to deal with connection.
alembic.operations.Operations.execute

execute(sql, execution_options=None)

使用当前迁移上下文执行给定的 SQL.

Execute the given SQL using the current migration context.

在 SQL 脚本上下文中,语句直接发送到输出流.但是,没有返回结果,因为此函数旨在生成可以在离线"模式下运行的更改脚本.

In a SQL script context, the statement is emitted directly to the output stream. There is no return result, however, as this function is oriented towards generating a change script that can run in "offline" mode.

参数:sql – 任何合法的 SQLAlchemy 表达式,包括:

Parameters: sql – Any legal SQLAlchemy expression, including:

  • 一个字符串 sqlalchemy.sql.expression.text() 构造.
  • 一个 sqlalchemy.sql.expression.insert() 构造.
  • 一个 sqlalchemy.sql.expression.update(),
  • sqlalchemy.sql.expression.insert() 或
  • sqlalchemy.sql.expression.delete() 构造.几乎任何东西这是可执行的",如 SQL 表达式语言教程中所述.

这篇关于修改数据作为 alembic 升级的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆