一个ORM列可以在SQLAlchemy中触发会话刷新吗?

时间:2013-02-10 22:14:15

标签: python postgresql orm sqlalchemy

问题

属性访问是否可以在SQLAlchemy中触发会话刷新?我的期望是,例如,通过column_property()或@hybrid_property附加到对象的查询导致会话autoflush,就像通过session.Query()进行的查询一样。情况似乎并非如此。

在下面的简单示例中,帐户包含Entry集合。它还提供了一个“balance”属性,由column_property()构成,它公开了一个select-sum查询。如果明确调用session.flush(),则新条目仅出现在帐户的余额中。

这种行为似乎不是最理想的:Account类的用户需要在知道balance实现内部的基础上,在整个代码中调用flush()调用。如果实现发生了变化 - 例如,如果“balance”以前是Python @property ---即使Account接口基本相同,也可以引入bug。还有其他选择吗?

完成示例

import sys
import sqlalchemy as sa
import sqlalchemy.sql
import sqlalchemy.orm
import sqlalchemy.ext.declarative

Base = sa.ext.declarative.declarative_base()

class Entry(Base):
    __tablename__ = "entries"

    id = sa.Column(sa.Integer, primary_key=True)
    value = sa.Column(sa.Numeric, primary_key=True)
    account_id = sa.Column(sa.Integer, sa.ForeignKey("accounts.id"))
    account = sa.orm.relationship("Account", backref="entries")

class Account(Base):
    __tablename__ = "accounts"

    id = sa.Column(sa.Integer, primary_key=True)
    balance = sa.orm.column_property(
        sa.sql.select([sa.sql.func.sum(Entry.value)])
            .where(Entry.account_id == id)
        )

def example(database_url):
    # connect to the database and prepare the schema
    engine = sa.create_engine(database_url)
    session = sa.orm.sessionmaker(bind=engine)()

    Base.metadata.create_all(bind = engine)

    # add an entry to an account
    account = Account()

    account.entries.append(Entry(value = 42))

    session.add(account)

    # and look for that entry in the balance
    print "account.balance:", account.balance

    assert account.balance == 42

if __name__ == "__main__":
    example(sys.argv[1])

观察到的输出

$ python sa_column_property_example.py postgres:///za_test
account.balance: None
Traceback (most recent call last):
  File "sa_column_property_example.py", line 46, in <module>
    example(sys.argv[1])
  File "sa_column_property_example.py", line 43, in example
    assert account.balance == 42
AssertionError

首选输出

我想看看“account.balance:42”,而没有添加对session.flush()的显式调用。

1 个答案:

答案 0 :(得分:4)

column_property仅在查询时评估,即当您说查询(帐户)时,以及属性过期时,即如果您说session.expire(“account”,['balance'])

要让属性每次都调用一个查询,我们使用@property(这里的一些小mod用于脚本与sqlite一起使用):

import sys
import sqlalchemy as sa
import sqlalchemy.sql
import sqlalchemy.orm
import sqlalchemy.ext.declarative

Base = sa.ext.declarative.declarative_base()

class Entry(Base):
    __tablename__ = "entries"

    id = sa.Column(sa.Integer, primary_key=True)
    value = sa.Column(sa.Numeric)
    account_id = sa.Column(sa.Integer, sa.ForeignKey("accounts.id"))
    account = sa.orm.relationship("Account", backref="entries")

class Account(Base):
    __tablename__ = "accounts"

    id = sa.Column(sa.Integer, primary_key=True)

    @property
    def balance(self):
        return sqlalchemy.orm.object_session(self).query(
                    sa.sql.func.sum(Entry.value)
                ).filter(Entry.account_id == self.id).scalar()

def example(database_url):
    # connect to the database and prepare the schema
    engine = sa.create_engine(database_url, echo=True)
    session = sa.orm.sessionmaker(bind=engine)()

    Base.metadata.create_all(bind = engine)

    # add an entry to an account
    account = Account()

    account.entries.append(Entry(value = 42))

    session.add(account)

    # and look for that entry in the balance
    print "account.balance:", account.balance

    assert account.balance == 42

if __name__ == "__main__":
    example("sqlite://")

请注意,“冲洗”本身通常不是我们不必担心的事情; autoflush功能将确保每次query()进入数据库以获取结果时都会调用flush,因此确实会确保发生查询,这是我们的目标。

解决此问题的另一种方法是使用混合动力车。我建议在SQL Expressions as Mapped Attributes阅读所有三种方法的概述,其中列出了每种方法的权衡。