我有两个映射对象,父对象和子对象。
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')
Run Code Online (Sandbox Code Playgroud)
当孩子更新时,我不仅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()
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为我已经处于冲洗状态并且我得到了类似的东西
SAWarning: Attribute history events accumulated on 1 previously clean instance within inner-flush event handlers have been reset, and will not result in …
Run Code Online (Sandbox Code Playgroud)