Skip to content

Late evaluation of "secondaryjoin" #11359

Closed Answered by nmoreaud
nmoreaud asked this question in Usage Questions
Discussion options

You must be logged in to vote

Okay, I have found a way, probably not the most elegant one but it seems to work.
I store the aliased join in a cached static method, and reuse it in multiple lambdas:

from functools import cache
# ...

class User(Base):
    # ...

    @cache
    @staticmethod
    def _friendship_union():
        return (
            select(friendship.c.friend_a_id, friendship.c.friend_b_id)
        ).union(
            select(friendship.c.friend_b_id, friendship.c.friend_a_id)
        ).alias()

    all_friends = relationship(
        'User',
        secondary=lambda: User._friendship_union(),
        primaryjoin=lambda: User.id == User._friendship_union().c.friend_a_id,
        secondaryjoin=lambda: User.

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@CaselIT
Comment options

@nmoreaud
Comment options

@CaselIT
Comment options

@nmoreaud
Comment options

@CaselIT
Comment options

Answer selected by nmoreaud
Comment options

You must be logged in to vote
2 replies
@zzzeek
Comment options

@nmoreaud
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants