diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2020-08-05 04:49:57 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2020-08-05 04:49:57 +0000 |
commit | ba9380ef28871b2274ab0bab75e5efddf2ced467 (patch) | |
tree | a69613dca1434c25ed2b8bfb877338206213f4e4 /lib/sqlalchemy/orm/unitofwork.py | |
parent | c813fe1678c4edbce32f0652353ed70f0a14566f (diff) | |
parent | 14fdd6260a578488bdad95b738ea6af5c2fcd13c (diff) | |
download | sqlalchemy-ba9380ef28871b2274ab0bab75e5efddf2ced467.tar.gz |
Merge "Establish future behavior for Session cascade backrefs, bind"
Diffstat (limited to 'lib/sqlalchemy/orm/unitofwork.py')
-rw-r--r-- | lib/sqlalchemy/orm/unitofwork.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index 97eea4864..9c67130ce 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -21,6 +21,16 @@ from .. import util from ..util import topological +def _warn_for_cascade_backrefs(state, prop): + util.warn_deprecated_20( + '"%s" object is being merged into a Session along the backref ' + 'cascade path for relationship "%s"; in SQLAlchemy 2.0, this ' + "reverse cascade will not take place. Set cascade_backrefs to " + "False for the 2.0 behavior; or to set globally for the whole " + "Session, set the future=True flag" % (state.class_.__name__, prop) + ) + + def track_cascade_events(descriptor, prop): """Establish event listeners on object attributes which handle cascade-on-set/append. @@ -42,11 +52,17 @@ def track_cascade_events(descriptor, prop): prop = state.manager.mapper._props[key] item_state = attributes.instance_state(item) + if ( prop._cascade.save_update - and (prop.cascade_backrefs or key == initiator.key) + and ( + (prop.cascade_backrefs and not sess.future) + or key == initiator.key + ) and not sess._contains_state(item_state) ): + if key != initiator.key: + _warn_for_cascade_backrefs(item_state, prop) sess._save_or_update_state(item_state) return item @@ -101,9 +117,14 @@ def track_cascade_events(descriptor, prop): newvalue_state = attributes.instance_state(newvalue) if ( prop._cascade.save_update - and (prop.cascade_backrefs or key == initiator.key) + and ( + (prop.cascade_backrefs and not sess.future) + or key == initiator.key + ) and not sess._contains_state(newvalue_state) ): + if key != initiator.key: + _warn_for_cascade_backrefs(newvalue_state, prop) sess._save_or_update_state(newvalue_state) if ( |