Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

python - Modify data as part of an alembic upgrade

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...