igorbenav / FastAPI-boilerplate

An extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0, PostgreSQL and Redis.
MIT License
632 stars 76 forks source link

Replace SQLAlchemy & Pydantic with SQLModel #124

Closed S1SYPHOS closed 6 months ago

S1SYPHOS commented 8 months ago

Since the author of FastAPI created SQLModel in order to be able to reduce the amount of code being maintained by combining models of both libraries, how about replacing them with an SQLModel based solution? :wink

igorbenav commented 8 months ago

Hey, @S1SYPHOS, thanks for the issue!

FastAPI supports Pydantic V2 since June 2023, SQLAlchemy is at 2.0+ since January 2023. SQLModel got proper Pydantic V2 support just last month, SQLAlchemy 2.0 in November 2023.

At least for now, I think this is not the way to go (since Tiangolo is the solo maintainer and FastAPI is the priority, it's very likely that SQLModel will always be far behind Pydantic and SQLAlchemy). Specially since migrating from SQLAlchemy + Pydantic to SQLModel is easier than the other way around. Plus, since SQLModel is just a superset on SQLAlchemy and Pydantic, everything should work properly if you just migrate the models to SQLModel.

It's something I think a lot of people would like though, so I'm thinking about creating a minimal template using SQLModel. What do you think?

S1SYPHOS commented 8 months ago

Thanks for your thorough explanation, I wasn't quite aware of all points you raised. Also it makes me think about using SQLModel after all 😅

However, having a minimal template (or maybe a commented out example) for people to get going quickly would be super appreciated! 🙏

igorbenav commented 8 months ago

I think SQLModel is really cool, and it really makes stuff simpler, I stopped using it when FastAPI got Pydantic V2 and SQLModel had no support for it in sight. I still use it when I want to make quick stuff though (building POCs etc).

I'll start creating the SQLModel version as a solution for this issue!

S1SYPHOS commented 8 months ago

Thank you very much! May I also express my admiration for your fastcrud project while I'm at it, it's a really cool project!

epicwhale commented 8 months ago

One reason I use SqlAlchemy directly is because I believe SQLModel does not have support async (https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html) with SqlAlchemy 2.0, does it? is my understanding correct?

CHE1RON commented 8 months ago

As I'm getting more and more familiar with this project, I see the value of keeping both libraries separate 😅

igorbenav commented 7 months ago

Thank you very much! May I also express my admiration for your fastcrud project while I'm at it, it's a really cool project!

Thanks, @S1SYPHOS! Really appreciate it.

igorbenav commented 7 months ago

One reason I use SqlAlchemy directly is because I believe SQLModel does not have support async (https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html) with SqlAlchemy 2.0, does it? is my understanding correct?

I see no reason for it not to work, since it's just a superset of sqlalchemy with pydantic compatibility built in.

igorbenav commented 7 months ago

As I'm getting more and more familiar with this project, I see the value of keeping both libraries separate 😅

It's a really cool idea, I just think it's not there yet.

joaoflaviosantos commented 7 months ago

I'd like to express my admiration for this FastAPI-boilerplate project here. Congratulations, @igorbenav!

I also want to share my project fastapi-async-sqlmodel-boilerplate which was inspired/based on this project. In it, I've made the necessary migration from SQLAlchemy + Pydantic to SQLModel:

FastAPI Async SQLModel Boilerplate

I'm using it in production, and it's been great.

Hope it helps someone.

@epicwhale, @S1SYPHOS, @CHE1RON

igorbenav commented 7 months ago

Hey, @joaoflaviosantos, really cool. More of a Feature-based structure, plus the sqlmodel part, so another option for everyone!

igorbenav commented 6 months ago

Hey, guys, SQLModel-boilerplate is live and mentioned in the readme! I'd love for you to give it a try.

igorbenav commented 6 months ago

Closed by #137

CHE1RON commented 6 months ago

I'll have a look later, thanks!