Replies: 4 comments
|
Here's the sqlalchemy thread for anyone interested. As discussed, in my actual use case I have set |
This comment was marked as spam.
This comment was marked as spam.
|
Before trusting the Setup: fresh venv, Result: the exact await session.refresh(child, attribute_names=["parent_node", "sub_nodes"])
print(repr(child.parent_node)) # Node() <- not None, not the real parent
print("parent_node" in child.__dict__) # True
print(child.parent_node.id) # raises sqlalchemy.exc.MissingGreenlet
I don't have a fix for What actually works reliably, and what I'd use in production instead of from sqlalchemy.orm import selectinload
from sqlmodel import select
stmt = (
select(Node)
.where(Node.id == child.id)
.options(selectinload(Node.parent_node), selectinload(Node.sub_nodes))
)
child = (await session.exec(stmt)).one()This issues explicit, single-purpose SELECTs for the relationships through the real relationship loader (respecting |
|
Solid catch @itzzdev09 on the expired stub and You're completely right: Explicitly using |
Uh oh!
There was an error while loading. Please reload this page.
First Check
Commit to Help
Example Code
Description
In the example above, the node instance doesn't have
parent_nodeloaded properly and the field throws:InvalidRequestError: 'Node.parent_node' is not available due to lazy='raise'sub_nodesloads properly and has value of empty list[]Using:
sqlalchemy Version: 2.0.44
sqlmodel Version: 0.0.27
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.27
Python Version
3.13.5
Additional Context
I'ved opened a similar discussion in SqlAlchemy and determined that this is specific to SQLModel.
SQLAlchemy test scripts:
Output:
SQLModel testscript:
Output:
All reactions