I have a setup which look something like this
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)
and have set up events to watch for ParentChildRelation changes
@event.listens_for(ParentChildRelation, 'after_insert')
def parent_child_relation_inserted(mapper, connection, target):
# DO STUFF HERE
The problem I'm running into is it seems that SqlAlchemy does not fire off those events when I do something like (I'm using Flask SqlAlchemy):
topic = Topic(id=new_id)
new_parent = Topic.query.get(anotherId)
topic.parent = new_parent
db.session.add(topic)
db.session.commit()
I would assume that the event handlers should be getting called, but the after_insert
handler isn't getting called.
Is this a limitation of SqlAlchemy, or am I missing something?