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

Reply via email to