tiangolo / pydantic-sqlalchemy

Tools to convert SQLAlchemy models to Pydantic models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

custom orm instead of model translation?

collerek opened this issue · comments

Hi, I had the same problem with maintaining two sets of models and took inspiration from encode/orm (not finished) and ormantic (not maintained) and started ormar that uses encode/databases and sqlalchemy core under the hood, and can be used in async mode. @tiangolo feel free to check it out and let me know if you find it useful!

Hi @collerek,

before I ask my question, please note that I'm certainly not an expert in neither pydantic nor orm's. But I have noticed that there is an entire zoo out there for orm's (sqlalchemy, peewee, etc.), validation (pydantic seems most common) AND also frameworks for graphQL (https://graphql.org/code/#python).

Now, if I wanted to build a really cool API that does input validation with pydantic, queries a database with an orm, and also allows for graphQL queries - how would I do this? (Note: I have not actually performed this wizzard like task).
You could define an sqlalchemy model, then translate this into a pydantic model (this repo) and then translate this into a graphene model (https://github.com/graphql-python/graphene-pydantic) which can be used in FastAPI. The latter part could also be replaced by translation of pydantic into strawberry (https://strawberry.rocks/docs/integrations/pydantic) (not sure if this would integrate with FastAPI).

Now the problem I see - and please DO CORRECT me - if you take a custom orm that combines the orm part with pydantic, ideally you either also integrate those further translations to graphene or the new orm should by pydantic enough (maybe it already is?) to also work with these further translations.

Looking forward to your opinion!

Hi @camold ,

Each ormar.Model subclasses pydantic.BaseModel as fastapi expects a subclass of this object in its routes and responses.
That should probably be enough to make it work with other libraries but I'm not sure. Depend on the functionality of pydantic that is expected in graphene-pydantic etc.

You can try and and check, if something does not work you can open an issue in ormar and we can try to work this out. ;)

Hey all! I created SQLModel to have all the best ideas I could have to mix SQLAlchemy and Pydantic, and to replace this package that can only do so much to help a bit with just part of the problem. 🤓

https://sqlmodel.tiangolo.com/

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.