我有一个看起来像这样的设置
class Topic(db.Model):
__tablename__ = 'topic'
id = db.Column(db.Integer, primary_key=True)
parent = db.relationship(
'Topic',
secondary='parent_child_relation',
primaryjoin='Topic.id == ParentChildRelation.child_topic_id',
secondaryjoin='ParentChildRelation.parent_topic_id == Topic.id',
backref='children',
uselist=False
)
class ParentChildRelation(db.Model):
__tablename__ = 'parent_child_relation'
id = db.Column(db.Integer, primary_key=True)
child_topic_id = db.Column(db.Integer, db.ForeignKey('topic.id'), nullable=False)
parent_topic_id = db.Column(db.Integer, db.ForeignKey('topic.id'), nullable=False)
并设置了要监视ParentChildRelation更改的事件
@event.listens_for(ParentChildRelation, 'after_insert')
def parent_child_relation_inserted(mapper, connection, target):
# DO STUFF HERE
我遇到的问题是,当我执行某些操作(我正在使用Flask SqlAlchemy)时,似乎SqlAlchemy没有触发这些事件:
topic = Topic(id=new_id)
new_parent = Topic.query.get(anotherId)
topic.parent = new_parent
db.session.add(topic)
db.session.commit()
我认为应该调用事件处理程序,但不会调用after_insert
处理程序。
这是SqlAlchemy的限制,还是我错过了什么?