summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/attributes.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2018-08-01 12:01:59 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2018-08-01 18:06:48 -0400
commit19cd4d4bc01d6176a23c72d37634c8ee4acbac40 (patch)
tree2b799e9a421e18590101e1d2961f35d8c1f63137 /lib/sqlalchemy/orm/attributes.py
parent1c32206120b1a6555f8bb7a20a0c4c53ea2f52a8 (diff)
downloadsqlalchemy-19cd4d4bc01d6176a23c72d37634c8ee4acbac40.tar.gz
Handle association proxy delete and provide for scalar delete cascade
Fixed multiple issues regarding de-association of scalar objects with the association proxy. ``del`` now works, and additionally a new flag :paramref:`.AssociationProxy.cascade_scalar_deletes` is added, which when set to True indicates that setting a scalar attribute to ``None`` or deleting via ``del`` will also set the source association to ``None``. Change-Id: I1580d761571d63eb03a7e8df078cef97d265b85c Fixes: #4308
Diffstat (limited to 'lib/sqlalchemy/orm/attributes.py')
-rw-r--r--lib/sqlalchemy/orm/attributes.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py
index e9227362e..0bbe70655 100644
--- a/lib/sqlalchemy/orm/attributes.py
+++ b/lib/sqlalchemy/orm/attributes.py
@@ -673,7 +673,6 @@ class ScalarAttributeImpl(AttributeImpl):
def delete(self, state, dict_):
- # TODO: catch key errors, convert to attributeerror?
if self.dispatch._active_history:
old = self.get(state, dict_, PASSIVE_RETURN_NEVER_SET)
else:
@@ -682,7 +681,10 @@ class ScalarAttributeImpl(AttributeImpl):
if self.dispatch.remove:
self.fire_remove_event(state, dict_, old, self._remove_token)
state._modified_event(dict_, self, old)
- del dict_[self.key]
+ try:
+ del dict_[self.key]
+ except KeyError:
+ raise AttributeError("%s object does not have a value" % self)
def get_history(self, state, dict_, passive=PASSIVE_OFF):
if self.key in dict_:
@@ -742,7 +744,10 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl):
def delete(self, state, dict_):
old = self.get(state, dict_)
self.fire_remove_event(state, dict_, old, self._remove_token)
- del dict_[self.key]
+ try:
+ del dict_[self.key]
+ except:
+ raise AttributeError("%s object does not have a value" % self)
def get_history(self, state, dict_, passive=PASSIVE_OFF):
if self.key in dict_:
@@ -969,7 +974,9 @@ class CollectionAttributeImpl(AttributeImpl):
collection = self.get_collection(state, state.dict)
collection.clear_with_event()
- # TODO: catch key errors, convert to attributeerror?
+
+ # key is always present because we checked above. e.g.
+ # del is a no-op if collection not present.
del dict_[self.key]
def initialize(self, state, dict_):