support callable boundparam in primaryjoin?

Issue #3562 duplicate
Mike Bayer repo owner created an issue

lazyloading can be made to work for this case:

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 --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.

Comments (2)

  1. Log in to comment