refreshing deleted objects

Issue #241 resolved
Former user created an issue

At the moment if you have two objects that refer to the same row in a database but each exist in different sessions and one is deleted the other one doesn't pick up the changes if it is refreshed.

I would expect that if an object is refreshed from a row that doesn't exist it should at least raise an error.

e.g.

session = create_session()
session2 = create_session()
obj1 = session.query(MyMappedObj).get_by(id=1)
obj2 = session2.query(MyMappedObj).get_by(id=1)

session.delete(obj1)
session.flush()
session2.refresh(obj2)

To me this should raise an error. The following change in sqlalchemy/orm/query.py should handle this.

Index: lib/sqlalchemy/orm/unitofwork.py
===================================================================
--- lib/sqlalchemy/orm/unitofwork.py    (revision 1711)
+++ lib/sqlalchemy/orm/unitofwork.py    (working copy)
@@ -102,7 +102,10 @@

     def refresh(self, sess, obj):
         self._validate_obj(obj)
-        sess.query(obj.__class__)._get(obj._instance_key, reload=True)
+        if sess.query(obj.__class__)._get(obj._instance_key, reload=True) is None:
+            raise InvalidRequestError('Could not refresh %s object from '\
+                                      'database: %s' % (obj.__class__.__name__,+                                                        repr(obj)))

     def expire(self, sess, obj):
         self._validate_obj(obj)

Comments (2)

  1. Log in to comment