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

session.info:

https://docs.sqlalchemy.org/en/13/orm/session_api.html?highlight=session%20info#sqlalchemy.orm.session.Session.info


>     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. 

Session.info:    :)  

https://docs.sqlalchemy.org/en/13/orm/session_api.html?highlight=session%20info#sqlalchemy.orm.session.Session.params.info



> 
> 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
>  
> http://www.sqlalchemy.org/
>  
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/b5031f10-c2c8-4065-b968-3a55f2bf6daen%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/b5031f10-c2c8-4065-b968-3a55f2bf6daen%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/34cedbfb-ddc8-4c3d-a9b1-94e4e4a92b3a%40www.fastmail.com.

Reply via email to