]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
if a contextual session is established via MapperExtension.get_session
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Aug 2006 00:12:59 +0000 (00:12 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Aug 2006 00:12:59 +0000 (00:12 +0000)
(as it is using the sessioncontext plugin, etc), a lazy load operation
will use that session by default if the parent object is not
persistent with a session already.

CHANGES
lib/sqlalchemy/orm/properties.py
test/orm/mapper.py

diff --git a/CHANGES b/CHANGES
index 4459c31880eccaced38032dc88b0a448b1ef7cc1..0639cdd7a6261d4a4fb05d9aa867bddd42eaf425 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,10 @@
 step in case the given property references a non-compiled mapper
 (as it did in the case of the tutorial !)
 - [ticket:277] check for pg sequence already existing before create
+- if a contextual session is established via MapperExtension.get_session
+(as it is using the sessioncontext plugin, etc), a lazy load operation
+will use that session by default if the parent object is not 
+persistent with a session already.
 
 0.2.7
 - quoting facilities set up so that database-specific quoting can be
index 5551348747662dac6f26b75bafc101a37c055c9e..b5d66c160e6ea7d8f016625b51f793a2737eeff5 100644 (file)
@@ -389,7 +389,10 @@ class LazyLoader(PropertyLoader):
 
             session = sessionlib.object_session(instance)
             if session is None:
-                raise exceptions.InvalidRequestError("Parent instance %s is not bound to a Session; lazy load operation of attribute '%s' cannot proceed" % (instance.__class__, self.key))
+                try:
+                    session = mapper.object_mapper(instance).get_session()
+                except exceptions.InvalidRequestError:
+                    raise exceptions.InvalidRequestError("Parent instance %s is not bound to a Session, and no contextual session is established; lazy load operation of attribute '%s' cannot proceed" % (instance.__class__, self.key))
                 
             # if we have a simple straight-primary key load, use mapper.get()
             # to possibly save a DB round trip
index 49b5a0cbcb8ea5cf70fea551f98901e429ded82b..2704e8e7ccd6c4d3b01f73dcfc30372b621d3d45 100644 (file)
@@ -3,7 +3,7 @@ import testbase
 import unittest, sys, os
 from sqlalchemy import *
 import sqlalchemy.exceptions as exceptions
-
+from sqlalchemy.ext.sessioncontext import SessionContext
 from tables import *
 import tables
 
@@ -613,6 +613,18 @@ class LazyTest(MapperSuperTest):
             {'user_id' : 7, 'addresses' : (Address, [{'address_id' : 1}])},
             )
 
+    def testbindstosession(self):
+        ctx = SessionContext(create_session)
+        m = mapper(User, users, properties = dict(
+            addresses = relation(mapper(Address, addresses, extension=ctx.mapper_extension), lazy=True)
+        ), extension=ctx.mapper_extension)
+        q = ctx.current.query(m)
+        u = q.selectfirst(users.c.user_id == 7)
+        ctx.current.expunge(u)
+        self.assert_result([u], User,
+            {'user_id' : 7, 'addresses' : (Address, [{'address_id' : 1}])},
+            )
+
     def testorderby(self):
         m = mapper(Address, addresses)