Skip to content
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

Support SQLAlchemy models with hybrid properties #218

Closed
omrihar opened this issue May 13, 2019 · 4 comments
Closed

Support SQLAlchemy models with hybrid properties #218

omrihar opened this issue May 13, 2019 · 4 comments
Labels
answered feature New feature or request reviewed

Comments

@omrihar
Copy link

omrihar commented May 13, 2019

Is your feature request related to a problem? Please describe.
I would like to be able to return an SQLAlchemy model and have it cast to a Pydantic schema. The problem is that hybrid properties on SQLAlchemy do not appear in the __dict__ variable of the model and therefore do not appear in the vars() function.

Describe the solution you'd like
I would like json_encodable to support SQLAlchemy models with hybrid properties. It is possible to use the inspection function of SQLAlchemy to achieve this:

from sqlalchemy import inspect
...
insp = inspect(Item)
for field in insp.mapper.orm_all_descriptors:
   ...

however I'm not sure how to extract the field name from this procedure.

Describe alternatives you've considered
A different alternative, which @tiangolo mentioned, is to try to obtain the property by name bsed on the pydantic model. This will also work, if we use getattr. For example, if I have an Item instance called item with a hybrid property title, it's possible to get it by item.title or getattr(item, 'title'), but not by using item['title'] or item.get('title').

Additional context
I'm using sqlalchemy_utils to provide translations-aware models. This only works on PostgreSQL. The field in the table is called, for example title_tr which is a JSONB field that has keys as language names and as values the translations. Then the sqlalchemy_utils.TranslationHybrid is a property on the instance that takes the value of the title_tr['current_locale'].
See https://sqlalchemy-utils.readthedocs.io/en/latest/internationalization.html.

@tiangolo
Copy link
Owner

@omrihar I'm working on improving the way we handle this in a PR to Pydantic here: pydantic/pydantic#520

This would help to solve this, even for nested relationship/models, lists of results, etc.

@tiangolo
Copy link
Owner

FastAPI now has a deep integration with the new Pydantic ORM mode.

This includes support for hybrid properties, lazy-loading, and many other things.

Check the new docs here: https://fastapi.tiangolo.com/tutorial/sql-databases/

@omrihar
Copy link
Author

omrihar commented Jun 21, 2019

Hi @tiangolo , great work! I read through the very thorough docs and I really like it :) And orm_mode as well :) I guess this issue can be closed now... I'm thinking of refactoring my app (based on the fullstack cookiecutter) to reflect the changes.

@omrihar omrihar closed this as completed Jun 21, 2019
@tiangolo
Copy link
Owner

Great! Thanks for reporting back and closing the issue.

Just so you know, the cookiecutter is not updated yet with these new changes.

@tiangolo tiangolo changed the title [FEATURE] Support SQLAlchemy models with hybrid properties Support SQLAlchemy models with hybrid properties Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered feature New feature or request reviewed
Projects
None yet
Development

No branches or pull requests

2 participants