News for hackers. Stuff that matters.
Re:Migrating to SQLAlchemy 2.0 by danjac on Thursday February 18, 2021 @02:17PM (#26184210)
Not having touched this stack in a while: what is best current practice for wiring together sqlalchemy and flask (including unit tests)?
Re:Migrating to SQLAlchemy 2.0 by returningfory2 on Thursday February 18, 2021 @02:40PM (#26184464)
Potentially don't wire them together? That is, isolate them in separate parts of the code.

I always found Flask-SQLAlchemy a strange approach because it ties opposite and somewhat orthogonal parts of the request lifecycle together. When requests-to-data-transactions is a 1-1 relationship it's a convenience, but once you step out of that I think it's better to do things by hand.

My main app has a simple layer structure of "request > business logic > data layer". All of the business logic functions start a SQLAlchemy transaction using a Python decorator that is ~10 lines of SQLAlchemy code.

Re:Migrating to SQLAlchemy 2.0 by ergo14 on Thursday February 18, 2021 @02:22PM (#26184259)
Check out how things are wired with in this Pyramid cookie cutter.

https://github.com/Pylons/pyramid-cookiecutter-starter/blob/...

I think same approach should work well for flask - just tie the session to the request object on first access.

Side note: I think we met on IRC few years ago ;-)

Re:Migrating to SQLAlchemy 2.0 by luhn on Thursday February 18, 2021 @03:31PM (#26185079)
There is a lot going on in that cookie cutter. The “transaction” library for transaction management, wired into Pyramid with pyramid_tm, wired into SQLAlchemy with zope.sqlalchemy. Works nicely for Pyramid, but I don’t know how that translates to Flask.
Re:Migrating to SQLAlchemy 2.0 by jordic on Friday February 19, 2021 @12:01AM (#26189949)
Think on it like a middleware that wraps a transaction. It's fine, till you start consuming other Io based services (elastic/solar/...) And you find that this blocks one connection per request..
Re:Migrating to SQLAlchemy 2.0 by ergo14 on Thursday February 18, 2021 @04:30PM (#26185892)
Doesn't, I wanted to show how it can be wired to request.