proper SQLmodel approach for Many to Many subqueryLoad #1508
Answered
by
YuriiMotov
SteffRainville
asked this question in
Questions
First Check
Commit to Help
Example Code@router.get("/user/", response_model=List[UserReadWithClients])
async def user(limit: int = 10, offset: int=0 ):
with Session(get_engine()) as session:
statement = select(User).offset(offset).limit(limit).options(subqueryload(User.clients))
results = session.exec(statement).all()
return resultsDescriptionI followed the Hero and Team sample it works well but I have a many to many relationship and I get the following message Parent instance <User at 0x22b6878e880> is not bound to a Session; lazy load operation of attribute 'clients' cannot proceed I use the sqlAlchem,y orm subquery load option to fix my problem but what is the proper SQLmodel approach for many to many? Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.4 Python Version3.9.7 Additional ContextNo response |
Answered by
YuriiMotov
Aug 13, 2025
Replies: 1 comment
|
I believe the approach with SQLModel is the same as with plain SQLAlchemy here. I would only consider using |
0 replies
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe the approach with SQLModel is the same as with plain SQLAlchemy here.
So, your solution is valid.
I would only consider using
selectinloadinstead ofsubqueryloadif there are no specific requirements that prevent you from that