fietsboek.models package

Main module for the fietsboek models.

Note that all SQLAlchemy models are re-imported here. You should only need to access the submodules if you need some of the auxiliary definitions.

fietsboek.models.get_engine(settings, prefix='sqlalchemy.')

Create an SQL Engine from the given settings.

fietsboek.models.get_session_factory(engine)

Create a session factory for the given engine.

fietsboek.models.get_tm_session(session_factory, transaction_manager, request=None)

Get a sqlalchemy.orm.Session instance backed by a transaction.

This function will hook the session to the transaction manager which will take care of committing any changes.

  • When using pyramid_tm it will automatically be committed or aborted depending on whether an exception is raised.

  • When using scripts you should wrap the session in a manager yourself. For example:

    import transaction
    
    engine = get_engine(settings)
    session_factory = get_session_factory(engine)
    with transaction.manager:
        dbsession = get_tm_session(session_factory, transaction.manager)
    

This function may be invoked with a request kwarg, such as when invoked by the reified .dbsession Pyramid request attribute which is configured via the includeme function below. The default value, for backwards compatibility, is None.

The request kwarg is used to populate the sqlalchemy.orm.Session’s “info” dict. The “info” dict is the official namespace for developers to stash session-specific information. For more information, please see the SQLAlchemy docs: https://docs.sqlalchemy.org/en/stable/orm/session_api.html#sqlalchemy.orm.session.Session.params.info

By placing the active request in the “info” dict, developers will be able to access the active Pyramid request from an instance of an SQLAlchemy object in one of two ways:

  • Classic SQLAlchemy. This uses the Session’s utility class method:

    from sqlalchemy.orm.session import Session as sa_Session
    
    dbsession = sa_Session.object_session(dbObject)
    request = dbsession.info["request"]
    
  • Modern SQLAlchemy. This uses the “Runtime Inspection API”:

    from sqlalchemy import inspect as sa_inspect
    
    dbsession = sa_inspect(dbObject).session
    request = dbsession.info["request"]
    
fietsboek.models.includeme(config)

Initialize the model for a Pyramid app.

Activate this setup using config.include('sqla_demo.models').

Submodules