This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sqlalchemy relationship fields are absent in router response #194
Comments
This is due to SQLAlchemy lazy-loading behavior. By the time a model is converted to a If you do anything that calls The current implementation takes whatever is returned by your path operation function, does it's best effort to convert it to something serializable with JSON (e.g. a And as the SQLAlchemy model doesn't really have a I'm thinking about changing the serialization implementation when there's a Pydantic model declared, to instead of trying to convert whatever is returned to a This would probably solve the SQLAlchemy lazy-loading issues. There's something else to have in mind. If FastAPI tried to extract the data directly without a Pydantic model, circular relationships would be a problem, as it would be trying to get But if there's a Pydantic model with some specific non-circular fields declared, FastAPI could make SQLAlchemy generate/fetch the needed data before serializing it. Would that work for you? |
Thank you for the detailed explanation. I hope your suggestion will help. As about circular relationships, i suppose i already encountered them even with proper Pydantic models, though i have to test it more... |
Just so you know, I just opened a PR in Pydantic: pydantic/pydantic#520 that would help us improve this. |
same issue parent = relationship("Parent", back_populates="children", lazy=False) |
to follow on that, here is a simplified gist of how i'm "translating" from sqlalchemy to pydantic I'm still new to python so i would appreciate any comments on how you guys are doing it. |
The latest Pydantic version includes "ORM mode", to solve these specific use cases. I just finished a deep integration of it into FastAPI, just released it in version Here are the new docs: https://fastapi.tiangolo.com/tutorial/sql-databases/ It should solve lazy-loading, hybrid-properties, dynamic attributes, relationships, and others. And it should work with all the ORMs, SQLAlchemy, Peewee, Tortoise ORM, GINO, etc. You declare the data you want to export in Pydantic models and they take care of extracting it from your ORM models. |
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
There is still an issue with sqlalchemy Data Models similar to the #4
If one has such models relationship as:
then the JSON response body of the API call does return only parent_id correct value and parent field has undefined value.
However when one uses a helper method like:
then response bode has all proper values.
UPDATE:
the same issue occurs with all SqlAlchemy hybrid_property fields.
The text was updated successfully, but these errors were encountered: