[Sqlalchemy-tickets] Issue #3562: support callable boundparam in primaryjoin? (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2015-10-23 15:51:43
|
New issue 3562: support callable boundparam in primaryjoin? https://bitbucket.org/zzzeek/sqlalchemy/issues/3562/support-callable-boundparam-in-primaryjoin Mike Bayer: lazyloading can be made to work for this case: ``` #!python def get_age(): return 15 class Child(Base): __tablename__ = 'child' id = Column('id', Integer, primary_key=True) age = Column(Integer) parent_id = Column(Integer, ForeignKey('parent.id')) class Parent(Base): __tablename__ = 'parent' id = Column('id', Integer, primary_key=True) @classmethod def __declare_last__(cls): cls.children_by_age = relationship( Child, lazy=True, primaryjoin=and_( Parent.id == Child.parent_id, Child.age == bindparam('age', callable_=get_age) ) ) ``` if we do this: ``` #!diff diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index 67dac1c..aa0a9f7 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -434,6 +434,8 @@ class LazyLoader(AbstractRelationshipLoader, util.MemoizedSlots): params = [] def visit_bindparam(bindparam): + if bindparam.callable: + return bindparam.unique = False if bindparam._identifying_key in bind_to_col: params.append(( ``` should we do that (and is that where we should do that)? Is there a better way to support bound callables in join conditions? what are the use cases for this ? note that the bindparam w/ callable_ works for joinedload, subqueryload, works for query.join(Parent.foo), so it's likely we should do this so that the behavior is consistent. |