SQLALchemy“after_insert”不更新目标对象字段

时间:2014-09-11 16:12:31

标签: postgresql python-2.7 flask sqlalchemy flask-sqlalchemy

我有一个模型(见下面的代码),我想在插入一个对象后执行一个函数,它将更新一个对象的字段。我使用 after_insert 映射器事件来执行此操作。

我已确认 after_insert 正确调用 event_extract_audio_text()处理程序,目标正在更新正确 audio_text 值。但是,一旦事件处理程序完成执行,就不会为数据库中的对象设置文本值。

代码

# Event handler
def event_extract_audio_text(mapper, connect, target):

    # Extract text from audio file
    audio_text = compute_text_from_audio_file(target.filename)

    # Update the 'text' field with extracted text
    target.audio_text = audio_text

# Model
class SoundsRaw(db.Model):
    __tablename__ = 'soundsraw'

    id = db.Column(db.BigInteger(), primary_key=True, autoincrement=True)
    filename = db.Column(db.String(255))
    audio_text = db.Column(db.Text())

# Event listener
event.listen(SoundsRaw, 'after_insert', event_extract_audio_text)

我还尝试调用 db.session.commit()尝试使用文本值更新对象,但随后我得到以下堆栈跟踪:

File "/Users/alexmarse/.virtualenvs/techmuseum/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 219, in _assert_active
raise sa_exc.ResourceClosedError(closed_msg)

ResourceClosedError: This transaction is closed

有什么想法吗?

软件版本

SQLAlchemy 0.9.4 烧瓶0.10.1 Flask-SQLAlchemy 1.0

2 个答案:

答案 0 :(得分:7)

关于' after_insert'处理程序的种类是直接使用连接。以下是我的表现方式:

class Link(db.Model):
    "News link data."

    __tablename__ = 'news_links'

    id = db.Column(db.BigInteger, primary_key=True)
    slug = db.Column(db.String, unique=True) #, nullable=False 

    url = db.Column(db.String, nullable=False, unique=True)
    title = db.Column(db.String)
    image_url = db.Column(db.String)
    description = db.Column(db.String)

@db.event.listens_for(Link, "after_insert")
def after_insert(mapper, connection, target):
    link_table = Link.__table__
    if target.slug is None:
        connection.execute(
            link_table.update().
            where(link_table.c.id==target.id).
            values(slug=slugify(target.id))
        )

答案 1 :(得分:1)

我最终通过放弃Mapper事件方法并使用Flask的Signalling Support来解决这个问题。

基本上,您可以在模型上注册“信号”,这些信号本质上是在发生特定类型的事件时调用的回调函数。就我而言,该事件是我模型的“更新”。

要配置信号,我将此方法添加到app.py文件中:

def on_models_committed(sender, changes):
    """Handler for model change signals"""
    for model, change in changes:
        if change == 'insert' and hasattr(model, '__commit_insert__'):
            model.__commit_insert__()
        if change == 'update' and hasattr(model, '__commit_update__'):
            model.__commit_update__()
        if change == 'delete' and hasattr(model, '__commit_delete__'):
            model.__commit_delete__()

然后,在我的模型上,我添加了此函数来处理更新事件:

 # Event methods
def __commit_update__(self):

    # create a new db session, which avoids the ResourceClosedError
    session = create_db_session()

    from techmuseum.modules.sensors.models import SoundsRaw
    # Get the SoundsRaw record by uuid (self contains the object being updated, 
    # but we can't just update/commit self -- we'd get a ResourceClosedError)
    sound = session.query(SoundsRaw).filter_by(uuid=self.uuid).first()

    # Extract text from audio file
    audio_text = compute_text_from_audio_file(sound)

    # Update the 'text' field of the sound
    sound.text = audio_text

    # Commit the update to the sound
    session.add(sound)
    session.commit()

def create_db_session():
    # create a new Session
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    psql_url = app.config['SQLALCHEMY_DATABASE_URI']
    some_engine = create_engine(psql_url)

    # create a configured "Session" class
    session = sessionmaker(bind=some_engine)

    return session