On Thu, May 30, 2013 at 3:28 PM, Claudio Freire <klaussfre...@gmail.com>
wrote:
> On Thu, May 30, 2013 at 2:25 PM, Michael Bayer <mike...@zzzcomputing.com>
wrote:
>>
>> On May 30, 2013, at 11:25 AM, Claudio Freire <klaussfre...@gmail.com>
wrote:
>>
>>>
>>> That way, one could use the second form up there and benefit from
>>> query hashing, because session/param binding wouldn't change the hash,
>>> and it would be a cache hit. Has it been explored already? Or maybe
>>> there's something wrong on how I'm using the compiled_cache thing?
>>> Should I start patching? ;-)
>>
>> there is a very wide gap between working with the baked query recipe,
for which I'm not aware of all the errors you refer to so more detail would
help
>
> Um... I don't remember the exact problems, will have to try it again.
> I just assumed it was targeted at an older SA release and gave up on
> it rather quickly.


So, this is what I'm getting, which is weird:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/usr/local/lib64/python2.7/site-packages/chorde-0.1-py2.7-linux-x86_64.egg/chorde/decorators.py",
line 184, in cached_f
    rv = f(*p, **kw)
  File "metrics.py", line 291, in action_metrics
    if action is not None and action.campaign is not None:
  File
"/usr/local/lib64/python2.7/site-packages/SQLAlchemy-0.7.10-py2.7-linux-x86_64.egg/sqlalchemy/orm/attributes.py",
line 458, in get
    value = self.callable_(state, passive)
  File
"/usr/local/lib64/python2.7/site-packages/SQLAlchemy-0.7.10-py2.7-linux-x86_64.egg/sqlalchemy/orm/strategies.py",
line 481, in _load_for_state
    (mapperutil.state_str(state), self.key)
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Action at
0x36f04d0> is not bound to a Session; lazy load operation of attribute
'campaign' cannot proceed

With:

 class CacheableQuery(sqlalchemy.orm.query.Query):

def __init__(self, *p, **kw):

self._cached_context = None

self._cached_context_labels = None

super(CacheableQuery, self).__init__(*p, **kw)

 @sqlalchemy.orm.query._generative()

def bake(self, labels=True):

self._compile_context(labels)


 def _compile_context(self, labels=True):

if self._cached_context and self._cached_context_labels == labels:

context = super(CacheableQuery, self)._compile_context(labels)

cached = self._cached_context
            context.statement = cached.statement
        else:

context = super(CacheableQuery, self)._compile_context(labels)

self._cached_context_labels = labels

self._cached_context = context

return context

All the rest in baked query recipe is already supported by SA, so I didn't
include it. Like with_session instead of from_session, and
execution_options() instead of hardcoding a cache.

Again, the usage is

q = blabla.bake()

...
q.with_session(S).params(..).first()

I have no idea why replacing the query messes the entity's session. Any
clue?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to