On 08/04/2016 10:14 AM, Florian Rüchel wrote:
I have a relationship that depends on a query time variable to determine
the correct join. The use case is request-time localization in a web
application. When running the query during a request, I want to
determine the locale and only load the translation for the current
language for a given object. However, the primaryjoin condition callable
is evaluated at mapping time instead which only happens once instead of
on every request.

Here is a quick sample:

def get_myobj_primaryjoin():
    return and_(MyObj.id == MyObjI18N.obj_id, request.locale ==
MyObjI18N.lang)


class MyObj(Base):
    id = Column(Integer, primary_key=True)
    _current_translation = relationship(MyObjI18N, uselist=False,
primaryjoin=get_myobj_primaryjoin, lazy='joined')


class MyObjI18N(Base):
    obj_id = Column(ForeignKey(MyObj.id), primary_key=True)
    lang = Column(String)

This should give a rough idea of the issue: request.locale changes at
query time, that is, if I do MyObj.query in two different requests, it
won't work, it will always take the first time it was called.

Note that I was previously using a with_transformation approach when
building the query but I wanted to remove the necessity to add that
every time a build a query and would have it much rather built implicitly.

Any ideas are highly appreciated, no argument I can pass to
"relationship" seems to help my use case.


we use a bound parameter for this and a recipe for getting a value in there can be seen at https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/GlobalFilter . In particular the lazyload case can only be affected using a custom MapperOption as described near the bottom of that recipe.




--
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
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to