SQL ALCHEMY instantly refresh

1,727 views
Skip to first unread message

Christian Démolis

unread,
Sep 17, 2009, 9:18:44 AM9/17/09
to sqlal...@googlegroups.com
How can i force sqlalchemy to refresh an object when i did a session.query???

Sqlalchemy seems to work with a cache, i want to deal with it.

Nota : i use sqlalchemy in non transactional mode
Session = scoped_session(sessionmaker(autocommit=True, bind=engine))

Alexandre Conrad

unread,
Sep 17, 2009, 12:12:10 PM9/17/09
to sqlal...@googlegroups.com
2009/9/17 Christian Démolis <christia...@gmail.com>:

> How can i force sqlalchemy to refresh an object when i did a
> session.query???

You may want look at this:

http://www.sqlalchemy.org/docs/05/session.html#refreshing-expiring

You may, as well, look at "expunging".

Alex

Christian Démolis

unread,
Sep 17, 2009, 12:51:37 PM9/17/09
to sqlal...@googlegroups.com
Bonjour,

Tu es français je pense au vu de ton prénom.
Je continue donc en français.

En fait j'ai 25000 lignes de codes derrière moi et j'aimerai éviter d'avoir à ajouter tous les refresh ou les session.query().populate_existing() partout dans mon code

J'ai tenté en vain de surcharger la méthode query de sqlalchemy mais je n'ai pas réussi...

J'ai trouvé ce lien qui est intéressant mais je n'ai pas réussi à l'exploiter :
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/PreFilteredQuery


2009/9/17 Alexandre Conrad <alexandr...@gmail.com>

Alexandre Conrad

unread,
Sep 17, 2009, 4:43:46 PM9/17/09
to sqlal...@googlegroups.com
Christian,

2009/9/17 Christian Démolis <christia...@gmail.com>:


> Bonjour,
>
> Tu es français je pense au vu de ton prénom.
> Je continue donc en français.

Nice guess.

I understand it feels more comfortable writing in French rather than
in English, but many people are reading this list (or is only Mike
doing support? ;) ) and may be interested at the topic. And more eyes
and brains may answer your question. So please keep conversations on
this list to its native language - English. If you really want to
switch to a non-English language with someone particular, please
exchange off-list, but I believe you'll dramatically reduce your
chances of solving your problem.

So for the record, you were explaining that you have 25000 lines of
code and you'd like to avoid to add refresh or
session.query().populate_existing() all around the place. You have
attempted to override the query method but couldn't make it. You have
pointed out the PreFilteredQuery
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/PreFilteredQuery but
was unsuccessful achieving what you wanted.

Sorry, maybe someone else can help you there. I haven't played with
Query overriding myself.

Alex

Michael Bayer

unread,
Sep 17, 2009, 5:20:59 PM9/17/09
to sqlal...@googlegroups.com
a Query subclass which just says

def __init__(*arg, **kw):
self._populate_existing = True
super(MyQuery, self).__init__(*arg, **kw)

should do it....





>
> Alex
>
> >
>

Christian Démolis

unread,
Sep 18, 2009, 3:57:21 AM9/18/09
to sqlal...@googlegroups.com
Hello,

Thx for the answer, thx to Alexandre to translate my mail.

Sorry, i continue in english, i tried to do that at the end of my declaration file :

Base.metadata.create_all(engine)
import sqlalchemy.orm.query
class MyQuery(sqlalchemy.orm.query.Query):

    def __init__(*arg, **kw):
       self._populate_existing = True
       super(MyQuery, self).__init__(*arg, **kw)
Session = scoped_session(sessionmaker(autocommit=True, bind=engine))
session = Session()
print "Le temps de déclaration SQL ALCHEMY", time.time()-xref

Is it correct? i m not very good in subclassing :S
It seems to not work.
When i add a print in the __init__, i never see it during the execution of my program so MyQuery is not used i think.
Can u tell me where should i subclass Query? In the declaration.py? in module sqlalchemy?
I tried to add myquery here
Session = scoped_session(sessionmaker(autocommit=True, bind=engine, query_cls=MyQuery))
but it does error
  File "C:\Python25\lib\site-packages\sqlalchemy-0.5.6-py2.5.egg\sqlalchemy\orm\
session.py", line 899, in query
    return self._query_cls(entities, self, **kwargs)
  File "Z:\Declaration.py", line 1451, in __init__
    self._populate_existing = True
NameError: global name 'self' is not defined

I can t touch to the sqlalchemy module because the interpreter and libraries are installed on multiple computers which execute one unique code on a shared network path.
So it's more easy to change my source code than sqlalchemy code...

i just want to change session.query behavior without change the code of sqlalchemy itself, please help me.

2009/9/17 Michael Bayer <mik...@zzzcomputing.com>

Alexandre Conrad

unread,
Sep 18, 2009, 4:06:10 AM9/18/09
to sqlal...@googlegroups.com
In Python, you have to pass "self" as first argument to all methods of a class:

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

def __init__(self, *arg, **kw):
...

Alex

2009/9/18 Christian Démolis <christia...@gmail.com>:

Christian Démolis

unread,
Sep 18, 2009, 4:50:16 AM9/18/09
to sqlal...@googlegroups.com
^^

I m so shameful

It works very well now.

I post the subclass complete if anyone need it in future

Base.metadata.create_all(engine)
import sqlalchemy.orm.query

from sqlalchemy.orm.query import Query
class Query(Query):
    def __init__(self, *arg, **kw):
       print "I pass here"
       self._populate_existing = True
       super(Query, self).__init__(*arg, **kw)

Session = scoped_session(sessionmaker(autocommit=True, bind=engine, query_cls=Query))
session = Session()

The new query class must be passed to the session call.

thx for all

2009/9/18 Alexandre Conrad <alexandr...@gmail.com>
Reply all
Reply to author
Forward
0 new messages