]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added extra fk override test
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 10 Jan 2008 18:05:20 +0000 (18:05 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 10 Jan 2008 18:05:20 +0000 (18:05 +0000)
- proper error message is raised when trying to
access expired instance attributes with no session
present

CHANGES
lib/sqlalchemy/orm/mapper.py
test/engine/reflection.py
test/orm/expire.py

diff --git a/CHANGES b/CHANGES
index 6f8ba13b20b8c170d812cc1fea22399f6e54716f..c0a63a546d2365970e81b97b8186aa6ebf76142e 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,7 +4,11 @@ CHANGES
 
 0.4.2p4
 ------
-
+- orm
+    - proper error message is raised when trying to 
+      access expired instance attributes with no session
+      present
+      
 - dialects
     - finally added PGMacAddr type to postgres 
       [ticket:580]
index 19e9c35e96fc4f02f9d1045677d3ad5f39e10d65..dcfae524f6b6866c9f3ea9615999e44ddc1e52d2 100644 (file)
@@ -1529,8 +1529,16 @@ def _load_scalar_attributes(instance, attribute_names):
     global object_session
     if not object_session:
         from sqlalchemy.orm.session import object_session
+
+    session = object_session(instance)
+    mapper = object_mapper(instance)
+    if not session:
+        try:
+            session = mapper.get_session()
+        except exceptions.InvalidRequestError:
+            raise exceptions.InvalidRequestError("Instance %s is not bound to a Session, and no contextual session is established; attribute refresh operation cannot proceed" % (instance.__class__))
         
-    if object_session(instance).query(object_mapper(instance))._get(instance._instance_key, refresh_instance=instance._state, only_load_props=attribute_names) is None:
+    if session.query(mapper)._get(instance._instance_key, refresh_instance=instance._state, only_load_props=attribute_names) is None:
         raise exceptions.InvalidRequestError("Could not refresh instance '%s'" % instance_str(instance))
 
 def _state_mapper(state, entity_name=None):
index a3b42bf6eca6c97e8a712440c8c602a324098ab8..ea56e315b0674b2dbfac8793f59e6f4ace0ab786 100644 (file)
@@ -129,8 +129,8 @@ class ReflectionTest(PersistTest):
             meta.drop_all()
 
     def test_override_create_fkcols(self):
-        """test that you can override columns and create new foreign keys to other reflected tables.
-        this is common with MySQL MyISAM tables."""
+        """test that you can override columns and create new foreign keys to other reflected tables
+        which have no foreign keys.  this is common with MySQL MyISAM tables."""
 
         meta = MetaData(testbase.db)
         users = Table('users', meta,
@@ -184,6 +184,7 @@ class ReflectionTest(PersistTest):
         finally:
             meta.drop_all()
 
+
     def test_unknown_types(self):
         meta = MetaData(testbase.db)
         t = Table("test", meta,
@@ -299,6 +300,25 @@ class ReflectionTest(PersistTest):
             assert [c.parent for c in a2.c.user_id.foreign_keys] == [a2.c.user_id]
             assert list(a2.c.user_id.foreign_keys)[0].parent is a2.c.user_id
             assert u2.join(a2).onclause == u2.c.id==a2.c.user_id
+
+            meta2 = MetaData(testbase.db)
+            u2 = Table('users', meta2, 
+                Column('id', Integer, primary_key=True),
+                autoload=True)
+            a2 = Table('addresses', meta2,
+                Column('id', Integer, primary_key=True),
+                Column('user_id',Integer, ForeignKey('users.id')),
+                autoload=True)
+
+            assert len(a2.foreign_keys) == 1
+            assert len(a2.c.user_id.foreign_keys) == 1
+            assert len(a2.constraints) == 2
+            assert [c.parent for c in a2.foreign_keys] == [a2.c.user_id]
+            assert [c.parent for c in a2.c.user_id.foreign_keys] == [a2.c.user_id]
+            assert list(a2.c.user_id.foreign_keys)[0].parent is a2.c.user_id
+            assert u2.join(a2).onclause == u2.c.id==a2.c.user_id
+
+
         finally:
             meta.drop_all()
 
index 2b25c5ba93869d9ad8b547d26bdbf1b943e01e00..d54e9fc2d353e4e1f75a91a6ef961dbc994a9469 100644 (file)
@@ -64,6 +64,18 @@ class ExpireTest(FixtureTest):
         sess.clear()
         assert sess.query(User).get(7).name == 'somenewname'
     
+    def test_no_session(self):
+        mapper(User, users)
+        sess = create_session()
+        u = sess.query(User).get(7)
+        
+        sess.expire(u, attribute_names=['name'])
+        sess.expunge(u)
+        try:
+            u.name
+        except exceptions.InvalidRequestError, e:
+            assert str(e) == "Instance <class 'testlib.fixtures.User'> is not bound to a Session, and no contextual session is established; attribute refresh operation cannot proceed"
+        
     def test_expire_preserves_changes(self):
         """test that the expire load operation doesn't revert post-expire changes"""