Modify data as part of an alembic upgrade

∥☆過路亽.° 提交于 2019-11-27 13:39:27

问题


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

with the following error:

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?


回答1:


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.

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.

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 looks like SQLAlchemy session object - it should have connection bind too.

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.

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.

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)

Execute the given SQL using the current migration context.

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.

Parameters: sql – Any legal SQLAlchemy expression, including:

  • a string a sqlalchemy.sql.expression.text() construct.
  • a sqlalchemy.sql.expression.insert() construct.
  • a sqlalchemy.sql.expression.update(),
  • sqlalchemy.sql.expression.insert(), or
  • sqlalchemy.sql.expression.delete() construct. Pretty much anything that’s “executable” as described in SQL Expression Language Tutorial.



回答2:


Its worth noting that if you do this, you probably want to freeze a copy of your orm model inside the migration, like this:

class MyType(Base):
  __tablename__ = 'existing_table'
  __table_args__ = {'extend_existing': True}
  id = Column(Integer, ...)
  ..

def upgrade():
  Base.metadata.bind = op.get_bind()

  for item in Session.query(MyType).all():
    ...

Otherwise you'll inevitably end up in a situation where you orm model changes and previous migrations no longer work.

Particularly note that you want to extend Base, not the base type itself (app.models.MyType) because your type might go away as some point, and once again, your migrations will fail.




回答3:


You need to import Base also and then

Base.metatada.bind = op.get_bind()

and after this you can use your models like always without errors.



来源:https://stackoverflow.com/questions/11970555/modify-data-as-part-of-an-alembic-upgrade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!