Replies: 8 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? |
Beta Was this translation helpful? Give feedback.
-
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... |
Beta Was this translation helpful? Give feedback.
-
Just so you know, I just opened a PR in Pydantic: pydantic/pydantic#520 that would help us improve this. |
Beta Was this translation helpful? Give feedback.
-
same issue parent = relationship("Parent", back_populates="children", lazy=False) |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions