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

A 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 Session is bound.

The “bind” is usually an instance of _engine.Engine, except in the case where the Session has been explicitly bound directly to a _engine.Connection.

For a multiply-bound or unbound Session, the mapper or clause arguments 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 a Session.query(), each individual INSERT/UPDATE/DELETE operation within a Session.flush(), call, etc.

The order of resolution is:

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

  2. if clause given and Session.binds is present, locate a bind based on _schema.Table objects found in the given clause present in Session.binds.

  3. if Session.binds is present, return that.

  4. if clause given, attempt to return a bind linked to the _schema.MetaData ultimately associated with the clause.

  5. if mapper given, attempt to return a bind linked to the _schema.MetaData ultimately associated with the _schema.Table or other selectable to which the mapper is mapped.

  6. No bind can be found, UnboundExecutionError is raised.

Note that the Session.get_bind() method can be overridden on a user-defined subclass of Session to 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.Mapper first by consulting the “binds” map associated with this Session, and secondly by consulting the _schema.MetaData associated with the _schema.Table to which the _orm.Mapper is mapped for a bind.

  • clause – A _expression.ClauseElement (i.e. _expression.select(), _expression.text(), etc.). If the mapper argument is not present or could not produce a bind, the given expression construct will be searched for a bound element, typically a _schema.Table associated with bound _schema.MetaData.

See also

session_partitioning

:paramref:`.Session.binds`

Session.bind_mapper()

Session.bind_table()