if an object is already attached to a session, you can get at that  
via object_session(instance).  if the object is detached, itll return  
None.

if you want better ways to handle getting a session for objects that  
arent yet attached to anything, thats all worked out by the  
SessionContext extension, and optionally the get_session() method on  
MapperExtension.  this allows you to pull up a session from a class'  
mapper which is based on *some* context, of which you can define to  
be whatever you want.  that whole thing is described at http:// 
www.sqlalchemy.org/docs/plugins.myt#plugins_sessioncontext .

typically the context is that Sessions are keyed to the identifiers  
of threads, since the Session is generally not threadsafe.



On Jul 13, 2006, at 2:52 PM, Randall Smith wrote:

> I see.  Would it be bad design to give an SA class instance a session
> attribute or something similar?  Not having it limits some work on  
> that
> object to the current scope.  Consider this:
>
> import util
> from sqla_classes import TestClass
>
> session = create_session(...)
> obj = session.query(TestClass).select()[0]
> util.doStuff(obj)
>
> The doStuff method of util does not have access to session unless  
> passed
> explicitly.  It would be convenient if obj knew what session it was a
> part of.  Maybe that would introduce some problems.  I don't know.
> Unless it has been detached, it should be in a session.
>
> I frequently find myself wanting to get the session or engine from  
> an SA
> class instance because it's inconvenient or difficult to get the  
> session
> or engine otherwise and I don't want to make my code dependent on how
> the connection is implemented.  For example, this
>
>      class_mapper(someclass).mapped_table.metadata.engine
>
> will only succeed if I connected metadata to the engine.  I may decide
> part way through a project to connect via create_session 
> (bind_to=engine)
> and my code would break.
>
> Randall
>
>
> Michael Bayer wrote:
>> if you dont have a session to work with then youd just use
>> connections off the engine being used.
>>
>> if you have your engines bound to tables and you want to get at that
>> via the mapped class, first off id say why not just have the engine
>> by itself somewhere?  but if you really need to, it looks like
>>
>>      class_mapper(someclass).mapped_table.metadata.engine
>>
>> for some examples about how to mix connections/sessions/transactions,
>> see
>>
>> http://www.sqlalchemy.org/docs/unitofwork.myt#unitofwork_transaction
>> http://www.sqlalchemy.org/docs/ 
>> unitofwork.myt#unitofwork_transaction_sql
>> http://www.sqlalchemy.org/docs/
>> unitofwork.myt#unitofwork_transaction_using
>>
>
>
>
> ---------------------------------------------------------------------- 
> ---
> Using Tomcat but need to do more? Need to support web services,  
> security?
> Get stuff done quickly with pre-integrated technology to make your  
> job easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache  
> Geronimo
> http://sel.as-us.falkag.net/sel? 
> cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> Sqlalchemy-users mailing list
> Sqlalchemy-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to