On Mar 22, 2011, at 11:16 AM, Serge Koval wrote:
> Hello,
>
> According to docs, I can use class level methods and variables with the
> ScopedSession:
> Since the Session() constructor now returns the same Session object every
> time within the current thread, the object returned byscoped_session() also
> implements most of the Session methods and properties at the “class” level,
> such that you don’t even need to instantiate Session()
>
> However, when I tried this on 0.6.5 and 0.6.6 - it did not work. Looks like
> not all expected variables are exposed, or they were renamed and
> ScopedSession object was not behaving as would one expect from Session object.
> Basically, here is sample stack trace:
> File
> "C:\Python26\lib\site-packages\sqlalchemy-0.6.6-py2.6.egg\sqlalchemy\orm\query.py",
> line 1646, in one
> ret = list(self)
> File
> "C:\Python26\lib\site-packages\sqlalchemy-0.6.6-py2.6.egg\sqlalchemy\orm\query.py",
> line 1688, in __iter__
> self.session._autoflush()
> AttributeError: 'ScopedSession' object has no attribute '_autoflush'
>
> So, I decided to add all missing attributes by monkey patching the
> ScopedSession class for now.
>
> Essentially this did the trick:
> def patch_sqlalchemy():
> from sqlalchemy.orm.scoping import ScopedSession, makeprop
>
> for prop in ('_autoflush', 'hash_key', '_finalize_loaded'):
> setattr(ScopedSession, prop, makeprop(prop))
>
> patch_sqlalchemy()
>
> I don't know if it right solution, or class-level attributes are no longer
> supported, etc.
>
> Please advice.
Public attributes are exposed, this is explicit within scoping.py. It seems
like you are attempting to pass a scoped session object to the constructor of
Query. That's not a feature which has ever been implemented (or ever been
requested), so that won't work.
Here's a fairly silly recipe if you want to do that for now:
from sqlalchemy.orm.query import Query
class ContextualQuery(Query):
@property
def session(self):
return self._session()
@session.setter
def session(self, sess):
self._session = sess
>
> Thank you,
> Serge.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.