assembl.lib.read_write_session module¶
-
class
assembl.lib.read_write_session.ReadWriteSession(bind=None, autoflush=False, read_bind=None, readonly=False, **options)[source]¶ Bases:
sqlalchemy.orm.session.SessionA session that can divert read queries to a different engine Inspired by https://gist.github.com/adhorn/b84dc47175259992d406
-
get_bind(mapper=None, clause=None)[source]¶ Return a “bind” to which this
Sessionis bound.The “bind” is usually an instance of
_engine.Engine, except in the case where theSessionhas been explicitly bound directly to a_engine.Connection.For a multiply-bound or unbound
Session, themapperorclausearguments are used to determine the appropriate bind to return.Note that the “mapper” argument is usually present when
Session.get_bind()is called via an ORM operation such as aSession.query(), each individual INSERT/UPDATE/DELETE operation within aSession.flush(), call, etc.The order of resolution is:
if mapper given and :paramref:`.Session.binds` is present, locate a bind based first on the mapper in use, then on the mapped class in use, then on any base classes that are present in the
__mro__of the mapped class, from more specific superclasses to more general.if clause given and
Session.bindsis present, locate a bind based on_schema.Tableobjects found in the given clause present inSession.binds.if
Session.bindsis present, return that.if clause given, attempt to return a bind linked to the
_schema.MetaDataultimately associated with the clause.if mapper given, attempt to return a bind linked to the
_schema.MetaDataultimately associated with the_schema.Tableor other selectable to which the mapper is mapped.No bind can be found,
UnboundExecutionErroris raised.
Note that the
Session.get_bind()method can be overridden on a user-defined subclass ofSessionto provide any kind of bind resolution scheme. See the example at session_custom_partitioning.- Parameters
mapper – Optional
mapper()mapped class or instance of_orm.Mapper. The bind can be derived from a_orm.Mapperfirst by consulting the “binds” map associated with thisSession, and secondly by consulting the_schema.MetaDataassociated with the_schema.Tableto which the_orm.Mapperis mapped for a bind.clause – A
_expression.ClauseElement(i.e._expression.select(),_expression.text(), etc.). If themapperargument is not present or could not produce a bind, the given expression construct will be searched for a bound element, typically a_schema.Tableassociated with bound_schema.MetaData.
-