Cascading will be applied according to the *expunge* cascade
rule.
"""
-
+ self._validate_persistent(object)
for c in [object] + list(_object_mapper(object).cascade_iterator('expunge', object)):
- self.uow._remove_deleted(c)
- self._unattach(c)
+ if c in self:
+ self.uow._remove_deleted(c)
+ self._unattach(c)
def save(self, object, entity_name=None):
"""Add a transient (unsaved) instance to this ``Session``.
obj._sa_session_id = self.hash_key
def _unattach(self, obj):
- self._validate_attached(obj)
- del obj._sa_session_id
-
- def _validate_attached(self, obj):
- """Validate that the given object is either pending or
- persistent within this Session.
- """
-
if not self._is_attached(obj):
raise exceptions.InvalidRequestError("Instance '%s' not attached to this Session" % repr(obj))
+ del obj._sa_session_id
def _validate_persistent(self, obj):
"""Validate that the given object is persistent within this
echo = logging.echo_property()
def _remove_deleted(self, obj):
- if hasattr(obj, "_instance_key") and obj._instance_key in self.identity_map:
+ if hasattr(obj, "_instance_key"):
del self.identity_map[obj._instance_key]
try:
self.deleted.remove(obj)
s.user_name = 'some other user'
s.flush()
+ def test_expunge_cascade(self):
+ tables.data()
+ mapper(Address, addresses)
+ mapper(User, users, properties={
+ 'addresses':relation(Address, backref=backref("user", cascade="all"), cascade="all")
+ })
+ session = create_session()
+ u = session.query(User).filter_by(user_id=7).one()
+
+ # get everything to load in both directions
+ print [a.user for a in u.addresses]
+
+ # then see if expunge fails
+ session.expunge(u)
+
def test_transaction(self):
class User(object):pass
mapper(User, users)