SqlAlchemy: Attaching one model to another during a "before_insert" event -
i'm having problems connecting subordinate model parent model using event handler. subordinate model never saved sqlalchemy (possibly because added during flush). i'm trying figure out how make work. places kind of thing happen scattered thru code , want catch of them. i've worked around set
event, can see other places might want use code set
event not acceptable.
def assure_subordinate(mapper, connect, target): """ if try use address parent not have subordinate object connected, create 1 , attach address. """ if not (target.address.subordinate or parent.address.subordinate_id): address = target.address session = inspect(address).session sub = subordinate(account_id=target.account_id, name=address.name, email=address.email) address.subordinate = sub session.add(sub) event.listen(parent, 'before_insert', assure_subordinate, propagate=true) event.listen(parent, 'before_update', assure_subordinate, propagate=true)
i'm hoping simple, otherwise before_insert
events going terribly limited.
Comments
Post a Comment