From: Mike Bayer Date: Thu, 17 Aug 2006 00:12:59 +0000 (+0000) Subject: if a contextual session is established via MapperExtension.get_session X-Git-Tag: rel_0_2_8~47 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ef606acacbc22dc0d811362acc11e06934296438;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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. --- diff --git a/CHANGES b/CHANGES index 4459c31880..0639cdd7a6 100644 --- 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 diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 5551348747..b5d66c160e 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -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 diff --git a/test/orm/mapper.py b/test/orm/mapper.py index 49b5a0cbcb..2704e8e7cc 100644 --- a/test/orm/mapper.py +++ b/test/orm/mapper.py @@ -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)