【发布时间】:2016-06-28 10:53:37
【问题描述】:
我有两个映射对象,Parent 和 Child。
class Parent(Base):
__tablename__ = 'parent'
id = ...
name = ...
date_modified = Column(SA_DateTime, default=DateTime.now,
onupdate=DateTime.now, nullable=False)
class Child(Base):
__tablename__ = 'child'
id = ...
name = ...
date_modified = Column(SA_DateTime, default=DateTime.now,
onupdate=DateTime.now, nullable=False)
parent = relationship(Parent, backref='parent')
当一个孩子更新时,我不仅要更改Child.date_modified,还要更改Child.parent.date_modified。
我试着这样做:
@event.listens_for(Child, 'after_update')
def modified_listener(mapper, connection, target):
if object_session(target).is_modified(target, include_collections=False):
target.parent.date_modified = DateTime.now()
但这不起作用,因为我已经处于同花顺状态并且得到类似的东西
SAWarning: Attribute history events accumulated on 1 previously clean instance within inner-flush event handlers have been reset, and will not result in database updates. Consider using set_committed_value() within inner-flush event handlers to avoid this warning.
如何使用 SQLAlchemy 解决这个问题?
【问题讨论】:
标签: python sqlalchemy