Skip to content

Access Relationship (from table model) from Read Model #1493

Discussion options

You must be logged in to vote

I guess you already tried this?

class HeroRead(HeroBase):
    id: int
    powers: List[Power]

But there is a caveat: by the time the powers attribute is accessed in your code, your session might already be closed. You can solve this by loading powers up front. This worked for me:

from sqlalchemy.orm import joinedload

statement = select(Hero).options(joinedload(Hero.powers))
heroes = session.exec(statement).unique().all()

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants
Converted from issue

This discussion was converted from issue #250 on August 12, 2025 11:02.