Skip to content

How to add additional filters on default result of relationship with link model? #1008

Closed Answered by 20-buck-spin
20-buck-spin asked this question in Questions
Discussion options

You must be logged in to vote

After searching for some time, I seem to have found a solution. The trick is to use the sa_relationship_kwargs in the following way

class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    secret_name: str
    age: int | None = Field(default=None, index=True)

    teams: list[Team] = Relationship(back_populates="heroes", link_model=HeroTeamLink, 
               sa_relationship_kwargs={"primaryjoin": 'and_(HeroTeamLink.hero_id==Hero.id, HeroTeamLink.is_training==True)'
         )

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by 20-buck-spin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant