fastapi-sqlalchemy icon indicating copy to clipboard operation
fastapi-sqlalchemy copied to clipboard

query support a la Flask SQLAlchemy

Open acidjunk opened this issue 3 years ago • 4 comments

Whilst transitioning our stack to FastAPI I luckily found this package. Thanks for the work on this package; it's a nice and easy way to get a DB connection without the need for a dependency that injects it in all controllers.

Our old Flask project uses some functionality from flask-sqlalchemy, that seems absent in fastapi-sqlalchemy: a query property on all models. So instead of db.session.query(User).all() we are using User.query.all()

Are there any plans to support this kind of querying?

acidjunk avatar Oct 26 '20 22:10 acidjunk

FWIW: For now I'm using the https://github.com/absent1706/sqlalchemy-mixins which seems to work OK. After including the middleware in main.py I inject the functionality like this:

with db():
    # Enable all sqlalchemy mixin (like query support)
    BaseModel.set_session(db.session)

Whilst the Base model stuff looks like this:

Base = declarative_base()


class BaseModel(Base, SessionMixin):
    __abstract__ = True
    pass

acidjunk avatar Nov 02 '20 18:11 acidjunk

@acidjunk Hi, thanks for the input. Does sqlalchemy-mixins work with async? specifically, using https://github.com/encode/databases

conradogarciaberrotaran avatar Jan 27 '21 15:01 conradogarciaberrotaran

I don't know: probably not (yet)

Async stuff is alpha level: https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html

in the end we wrote our own sqlalchemy bindings; and we didn't use this library as it had to much boilerplate for managing connections. We also needed an easy way to run pytests tests with migrations applied to DB.

https://github.com/workfloworchestrator/orchestrator-core/blob/main/orchestrator/db/database.py

acidjunk avatar Mar 24 '21 19:03 acidjunk

@mfreeborn @conradogarciaberrotaran any updates on this issue? It would be very useful to have.

maksad avatar Jul 12 '22 09:07 maksad