diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-11-01 13:15:14 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-11-02 01:22:41 -0400 |
commit | 2e2af8d918a568ca3036f88e457caaf59f6af644 (patch) | |
tree | e9f2c307a79fe1a0111f702e10166aa6ba5021f3 /lib/sqlalchemy/orm/unitofwork.py | |
parent | 7d372da7385be6a9817a20b6b62f7c4237af7b26 (diff) | |
download | sqlalchemy-2e2af8d918a568ca3036f88e457caaf59f6af644.tar.gz |
Implement __delete__
A long-standing oversight in the ORM, the ``__delete__`` method for a many-
to-one relationship was non-functional, e.g. for an operation such as ``del
a.b``. This is now implemented and is equivalent to setting the attribute
to ``None``.
Fixes: #4354
Change-Id: I60131a84c007b0bf6f20c5cc5f21a3b96e954046
Diffstat (limited to 'lib/sqlalchemy/orm/unitofwork.py')
-rw-r--r-- | lib/sqlalchemy/orm/unitofwork.py | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index ae5cee8d2..a83a99d78 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -61,19 +61,22 @@ def track_cascade_events(descriptor, prop): if prop.uselist else "related attribute delete") - # expunge pending orphans - item_state = attributes.instance_state(item) + if item is not None and \ + item is not attributes.NEVER_SET and \ + item is not attributes.PASSIVE_NO_RESULT and \ + prop._cascade.delete_orphan: + # expunge pending orphans + item_state = attributes.instance_state(item) - if prop._cascade.delete_orphan and \ - prop.mapper._is_orphan(item_state): - if sess and item_state in sess._new: - sess.expunge(item) - else: - # the related item may or may not itself be in a - # Session, however the parent for which we are catching - # the event is not in a session, so memoize this on the - # item - item_state._orphaned_outside_of_session = True + if prop.mapper._is_orphan(item_state): + if sess and item_state in sess._new: + sess.expunge(item) + else: + # the related item may or may not itself be in a + # Session, however the parent for which we are catching + # the event is not in a session, so memoize this on the + # item + item_state._orphaned_outside_of_session = True def set_(state, newvalue, oldvalue, initiator): # process "save_update" cascade rules for when an instance |