FAQ or Feature Ideas for ORM Object and Session

11 views
Skip to first unread message

Jonathan Vanasco

unread,
Jan 27, 2021, 1:12:38 PM1/27/21
to sqlalchemy
I've been working with a handful of SQLAlchemy and Pyramid based projects recently, and two situations have repeatedly come up:

1. Given a SQLAlchemy Object, access the SQLAlchemy Session
2. Given a SQLAlchemy Object or Session, access the Pyramid Request object

The general solutions I've used to handle this is:

1. An Object can use the runtime inspection API to grab it's active session:

    from sqlalchemy import inspect

    @property
    def _active_session(self):
        dbSession = inspect(self).session
        return dbSession

2.  Attach the Pyramid request to the session_factory when a session is created:

    def get_tm_session(request):
dbSession = session_factory()
zope.sqlalchemy.register(dbSession, transaction_manager=transaction_manager, keep_session=True)
if request is not None:
def _cleanup(request):
dbSession.close()
request.add_finished_callback(_cleanup)
# cache our request onto the dbsession
dbSession.pyramid_request = request
return dbSession

I've needed to implement these patterns in a lot of projects. This makes me wonder if there is/could be a better way.


1.  Would it be beneficial if ORM objects could surface the current Session, if any, as a documented property ?  I do this in my base classes, but with the overhead of the inspect system, and I repeat this in every project.

2.  Would it be better for the sessionmaker had any of ?

    a. An official namespace were developers could attach information.  I'm using `pyramid_request` because I doubt SQLAlchemy will every step on that - but it would be nice if there were a dedicated attribute/object/namespace on the Session
    b. `sqlalchemy.orm.session.Session()` could accept a dict/payload/object/whatever on init, which would be attached to a single session in the aforementioned dedicated namespace. 

The usage would be something like:

    sess = Session(customized={"request": request})

which might then me accessed as:

    sess.customized.request






Mike Bayer

unread,
Jan 27, 2021, 2:25:29 PM1/27/21
to noreply-spamdigest via sqlalchemy


On Wed, Jan 27, 2021, at 1:12 PM, 'Jonathan Vanasco' via sqlalchemy wrote:
I've been working with a handful of SQLAlchemy and Pyramid based projects recently, and two situations have repeatedly come up:

1. Given a SQLAlchemy Object, access the SQLAlchemy Session
2. Given a SQLAlchemy Object or Session, access the Pyramid Request object

The general solutions I've used to handle this is:

1. An Object can use the runtime inspection API to grab it's active session:

    from sqlalchemy import inspect

    @property
    def _active_session(self):
        dbSession = inspect(self).session
        return dbSession

There's a much older function sqlalchemy.orm.object_session() that also does this.   I prefer giving people the inspect() interface because I'd rather expose the first class API and not confuse things.   but object_session() isn't going away.



2.  Attach the Pyramid request to the session_factory when a session is created:

    def get_tm_session(request):
dbSession = session_factory()
zope.sqlalchemy.register(dbSession, transaction_manager=transaction_manager, keep_session=True)
if request is not None:
def _cleanup(request):
dbSession.close()
request.add_finished_callback(_cleanup)
# cache our request onto the dbsession
dbSession.pyramid_request = request
return dbSession

I've needed to implement these patterns in a lot of projects. This makes me wonder if there is/could be a better way.

That request would be better placed in session.info which is the official dictionary for third-party things to go.




1.  Would it be beneficial if ORM objects could surface the current Session, if any, as a documented property ?  I do this in my base classes, but with the overhead of the inspect system, and I repeat this in every project.

as a property?  no, we can't do that.    we try to add zero "names" to the class of any kind.    there's "_sa_instance_state", "_sa_instrumentation_manager" and that's as far as we go; doing absolute zero to the namespace of the mapped class is a fundamental rule of the ORM.    



2.  Would it be better for the sessionmaker had any of ?

    a. An official namespace were developers could attach information.  I'm using `pyramid_request` because I doubt SQLAlchemy will every step on that - but it would be nice if there were a dedicated attribute/object/namespace on the Session



    b. `sqlalchemy.orm.session.Session()` could accept a dict/payload/object/whatever on init, which would be attached to a single session in the aforementioned dedicated namespace. 





The usage would be something like:

    sess = Session(customized={"request": request})

which might then me accessed as:

    sess.customized.request

poof! it's done










--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Jonathan Vanasco

unread,
Jan 27, 2021, 4:34:43 PM1/27/21
to sqlalchemy
Ok. I'll generate a docs PR for sqlalchemy and pyramid.   this comes up so much.
Reply all
Reply to author
Forward
0 new messages