]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added new helper function :func:`.was_deleted`, returns True
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Feb 2013 23:24:46 +0000 (18:24 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 20 Feb 2013 23:24:46 +0000 (18:24 -0500)
if the given object was the subject of a :meth:`.Session.delete`
operation.
- An object that's deleted from a session will be de-associated with
that session fully after the transaction is committed, that is
the :func:`.object_session` function will return None.
[ticket:2658]

doc/build/changelog/changelog_08.rst
doc/build/orm/session.rst
lib/sqlalchemy/orm/__init__.py
lib/sqlalchemy/orm/session.py
lib/sqlalchemy/orm/util.py
test/orm/test_session.py

index 296141c79914ffdca3440a2977581d400317b599..8951217ffb8bffef5f5d9714dc59d92418a91869 100644 (file)
@@ -6,6 +6,22 @@
 .. changelog::
     :version: 0.8.0
 
+    .. change::
+        :tags: feature, orm
+        :tickets: 2658
+
+      Added new helper function :func:`.was_deleted`, returns True
+      if the given object was the subject of a :meth:`.Session.delete`
+      operation.
+
+    .. change::
+        :tags: bug, orm
+        :tickets: 2658
+
+      An object that's deleted from a session will be de-associated with
+      that session fully after the transaction is committed, that is
+      the :func:`.object_session` function will return None.
+
     .. change::
         :tags: bug, oracle
 
index 00d67b512b4baf7d45d3fc2285a3ef7b688f3cb1..97d6f15a0edd1e45886792219d7d7238a2a5ab39 100644 (file)
@@ -1946,6 +1946,8 @@ Session Utilites
 
 .. autofunction:: object_session
 
+.. autofunction:: was_deleted
+
 Attribute and State Management Utilities
 -----------------------------------------
 
index e9dde3ca78e391573d9d022eae501769458eb63c..0132bb38972fa72211ddca1070b4b0dc85cf8a08 100644 (file)
@@ -35,6 +35,7 @@ from .util import (
      object_mapper,
      outerjoin,
      polymorphic_union,
+     was_deleted,
      with_parent,
      with_polymorphic,
      )
@@ -125,6 +126,7 @@ __all__ = (
     'undefer',
     'undefer_group',
     'validates',
+    'was_deleted',
     'with_polymorphic'
     )
 
index 00c6d4227f1e0da6b1baabcbb7bab1bb208159eb..2915fd4c8c1176b6e224119554e901a876fc5c3b 100644 (file)
@@ -255,6 +255,10 @@ class SessionTransaction(object):
         if not self.nested and self.session.expire_on_commit:
             for s in self.session.identity_map.all_states():
                 s._expire(s.dict, self.session.identity_map._modified)
+            for s in self._deleted:
+                s.session_id = None
+            self._deleted.clear()
+
 
     def _connection_for_bind(self, bind):
         self._assert_is_active()
index 492523e637580fe6d07af81abe337af37d7bf93c..cc9dd6ba5c8169eb0499e67f28cccdbcd0fa702d 100644 (file)
@@ -1210,9 +1210,30 @@ def _orm_columns(entity):
 
 
 def has_identity(object):
+    """Return True if the given object has a database
+    identity.
+
+    This typically corresponds to the object being
+    in either the persistent or detached state.
+
+    .. seealso::
+
+        :func:`.was_deleted`
+
+    """
     state = attributes.instance_state(object)
     return state.has_identity
 
+def was_deleted(object):
+    """Return True if the given object was deleted
+    within a session flush.
+
+    .. versionadded:: 0.8.0
+
+    """
+
+    state = attributes.instance_state(object)
+    return state.deleted
 
 def instance_str(instance):
     """Return a string describing an instance."""
index ff4091ebe6c036c4ce002673724b4cfd229e9306..0a50a48cb1df9525768868286df0235825da33a5 100644 (file)
@@ -12,7 +12,7 @@ from sqlalchemy import testing
 from sqlalchemy import Integer, String, Sequence
 from sqlalchemy.testing.schema import Table, Column
 from sqlalchemy.orm import mapper, relationship, backref, joinedload, \
-    exc as orm_exc, object_session
+    exc as orm_exc, object_session, was_deleted
 from sqlalchemy.util import pypy
 from sqlalchemy.testing import fixtures
 from test.orm import _fixtures
@@ -835,6 +835,24 @@ class SessionStateWFixtureTest(_fixtures.FixtureTest):
             assert sa.orm.object_session(a) is None
             assert sa.orm.attributes.instance_state(a).session_id is None
 
+    def test_deleted_expunged(self):
+        users, User = self.tables.users, self.classes.User
+
+        mapper(User, users)
+        sess = Session()
+
+        u1 = sess.query(User).first()
+        sess.delete(u1)
+
+        assert not was_deleted(u1)
+        sess.flush()
+
+        assert was_deleted(u1)
+        assert u1 not in sess
+        assert object_session(u1) is sess
+        sess.commit()
+
+        assert object_session(u1) is None
 
 
 class WeakIdentityMapTest(_fixtures.FixtureTest):