the backrefs intentionally don't keep fanning deep into object graph for this kind of thing, so if you want it to go one hop further you can add an event to do that directly:
from sqlalchemy import event from sqlalchemy.orm import attributes @event.listens_for(Address.user, "set") def _intercept_set(target, value, oldvalue, initiator): if isinstance(oldvalue, User) and "address" in oldvalue.__dict__: attributes.set_committed_value(oldvalue, "address", None) the "set_committed_value" is to avoid triggering any new events which will cause a recursion overflow. On Wed, Mar 28, 2018 at 10:49 AM, Serhii Mozghovyi <egnart...@gmail.com> wrote: > Is it possible to make child objects (many-to-one side) delete itself from > the old parent's collection when it is added to a different parent? > See the file attached. The old parent remains unaware that he doesn't have > this child anymore. > > P.S. session.expire() is an obvious solution but too heavy. I expect some > event-based collection synchronization, much like backref (back_populates) > connected collections. > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.