当我使用@observes时,会话已经刷新了

时间:2015-06-25 22:52:57

标签: python sqlalchemy flask-sqlalchemy

我正在构建一个烧瓶应用程序,并且我想使用sqlalchemy observers在该货件中的所有产品都可用后更新货件状态。

这是我的数据模型:

from app import db
from sqlalchemy_utils import observes

class Shipment(db.Model):
    __tablename__ = 'shipment'
    id = db.Column(db.Integer, primary_key=True)
    products = db.relationship('Product', backref='shipment', lazy='dynamic')
    all_products_ready = db.Column(db.Boolean)

    @observes('products')
    def product_observer(self, products):
        for p in self.products:
            if p.status != 'ready':
                self.all_products_ready = False
                return False
        self.all_products_ready = True
        return True

class Product(db.Model):
    __tablename__ = 'product'
    id = db.Column(db.Integer, primary_key=True)
    shipment_id = db.Column(db.Integer, db.ForeignKey('shipment.id'))

    status = db.Column(db.String(120), index=True)

这是我运行的一些代码来测试它:

shipment = models.Shipment(products=[models.Product(status='ready'), models.Product(status='not_ready')])
db.session.add(shipment)
db.session.commit()

print(shipment.all_products_ready)

当我运行此代码时,我得到一个InvalidRequestError:Session已经刷新了。

这是堆栈跟踪:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    db.session.commit()
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\scoping.py",
line 150, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\session.py",
line 788, in commit
    self.transaction.commit()
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\session.py",
line 384, in commit
    self._prepare_impl()
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\session.py",
line 364, in _prepare_impl
    self.session.flush()
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\session.py",
line 1985, in flush
    self._flush(objects)
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\session.py",
line 2012, in _flush
    self.dispatch.before_flush(self, flush_context, objects)
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\event\attr.py", l
ine 221, in __call__
    fn(*args, **kw)
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy_utils\observer.py
", line 272, in invoke_callbacks
    for (root_obj, func, objects) in args:
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy_utils\observer.py
", line 252, in gather_callback_args
    lambda obj: obj not in session.deleted
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy_utils\functions\o
rm.py", line 741, in getdotattr
    last = [v for v in last if condition(v)]
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\dynamic.py",
line 245, in __iter__
    sess = self.session
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\dynamic.py",
line 237, in session
    sess.flush()
  File "U:\dev\observertest\flask\lib\site-packages\sqlalchemy\orm\session.py",
line 1979, in flush
    raise sa_exc.InvalidRequestError("Session is already flushing")
sqlalchemy.exc.InvalidRequestError: Session is already flushing

如何在不收到此错误的情况下使用我的模型?

1 个答案:

答案 0 :(得分:1)

我不完全确定原因,但我认为使用dynamic加载关系会导致问题。在这一行:

    products = db.relationship('Product', backref='shipment', lazy='dynamic')

您需要将lazy参数更改为select而不是dynamic(或者您可以全部取出lazy参数,因为select是默认值)。

有关所有可用选项,请参阅the sqlalchemy reference