diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2018-11-02 13:21:15 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2018-11-02 13:21:15 +0000 |
commit | bc7c212370621a26629392cf247492c773fa63fd (patch) | |
tree | bc6dc5badc5e56aaf7f03e644d393826bf52ace4 /lib/sqlalchemy/orm/attributes.py | |
parent | e991684a39fa9fae2628ce583e141b9aea99d856 (diff) | |
parent | 2e2af8d918a568ca3036f88e457caaf59f6af644 (diff) | |
download | sqlalchemy-bc7c212370621a26629392cf247492c773fa63fd.tar.gz |
Merge "Implement __delete__"
Diffstat (limited to 'lib/sqlalchemy/orm/attributes.py')
-rw-r--r-- | lib/sqlalchemy/orm/attributes.py | 43 |
1 files changed, 33 insertions, 10 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index 745032e44..3eaa41e8f 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -674,7 +674,6 @@ class ScalarAttributeImpl(AttributeImpl): self._remove_token = Event(self, OP_REMOVE) def delete(self, state, dict_): - if self.dispatch._active_history: old = self.get(state, dict_, PASSIVE_RETURN_NEVER_SET) else: @@ -683,10 +682,12 @@ class ScalarAttributeImpl(AttributeImpl): if self.dispatch.remove: self.fire_remove_event(state, dict_, old, self._remove_token) state._modified_event(dict_, self, old) - try: - del dict_[self.key] - except KeyError: - raise AttributeError("%s object does not have a value" % self) + + existing = dict_.pop(self.key, NO_VALUE) + if existing is NO_VALUE and old is NO_VALUE and \ + not state.expired and \ + self.key not in state.expired_attributes: + raise AttributeError("%s object does not have a value" % self) def get_history(self, state, dict_, passive=PASSIVE_OFF): if self.key in dict_: @@ -744,11 +745,25 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl): __slots__ = () def delete(self, state, dict_): - old = self.get(state, dict_) + if self.dispatch._active_history: + old = self.get( + state, dict_, + passive=PASSIVE_ONLY_PERSISTENT | + NO_AUTOFLUSH | LOAD_AGAINST_COMMITTED) + else: + old = self.get( + state, dict_, passive=PASSIVE_NO_FETCH ^ INIT_OK | + LOAD_AGAINST_COMMITTED) + self.fire_remove_event(state, dict_, old, self._remove_token) - try: - del dict_[self.key] - except: + + existing = dict_.pop(self.key, NO_VALUE) + + # if the attribute is expired, we currently have no way to tell + # that an object-attribute was expired vs. not loaded. So + # for this test, we look to see if the object has a DB identity. + if existing is NO_VALUE and old is not PASSIVE_NO_RESULT and \ + state.key is None: raise AttributeError("%s object does not have a value" % self) def get_history(self, state, dict_, passive=PASSIVE_OFF): @@ -1382,6 +1397,10 @@ class History(History): # key situations if id(original) in _NO_STATE_SYMBOLS: deleted = () + # indicate a "del" operation occured when we don't have + # the previous value as: ([None], (), ()) + if id(current) in _NO_STATE_SYMBOLS: + current = None else: deleted = [original] if current is NEVER_SET: @@ -1398,7 +1417,7 @@ class History(History): return cls((), (), ()) else: return cls((), [current], ()) - elif current is original: + elif current is original and current is not NEVER_SET: return cls((), [current], ()) else: # current convention on related objects is to not @@ -1408,6 +1427,10 @@ class History(History): # ignore the None in any case. if id(original) in _NO_STATE_SYMBOLS or original is None: deleted = () + # indicate a "del" operation occured when we don't have + # the previous value as: ([None], (), ()) + if id(current) in _NO_STATE_SYMBOLS: + current = None else: deleted = [original] if current is NO_VALUE or current is NEVER_SET: |