sqlalchemy_helpers.aio module

Database management (async).

This must remain independent from any web framework.

sqlalchemy_helpers.aio.Base

SQLAlchemy’s base class for models.

Type:

object

class sqlalchemy_helpers.aio.AsyncDatabaseManager(uri, alembic_location, engine_args=None, base_model=None)[source]

Bases: DatabaseManager

Helper for a SQLAlchemy and Alembic-powered database, asynchronous version.

Parameters:
  • uri (str) – the database URI

  • alembic_location (str) – a path to the alembic directory

  • engine_args (dict) – additional arguments passed to create_async_engine

alembic_cfg

the Alembic configuration object

Type:

alembic.config.Config

engine

the SQLAlchemy Engine instance

Type:

sqlalchemy.engine.Engine

Session

the SQLAlchemy scoped session factory

Type:

sqlalchemy.orm.scoped_session

configured_connection(f)[source]
async create()[source]

Create the database tables.

async drop()[source]

Drop all the database tables.

async get_current_revision(session)[source]

Get the current alembic database revision.

async get_status()[source]

Get the status of the database.

Returns:

see DatabaseStatus.

Return type:

DatabaseStatus member

async sync()[source]

Create or update the database schema.

Returns:

see SyncResult.

Return type:

SyncResult member

async upgrade(target='head')[source]

Upgrade the database schema.

async sqlalchemy_helpers.aio.get_by_pk(pk, *, session, model)[source]

Get a model instance using its primary key.

Example: user = get_by_pk(42, session=session, model=User)

async sqlalchemy_helpers.aio.get_one(session: AsyncSession, model, **attrs) Base[source]

Get an object from the datbase.

Parameters:
  • session – The SQLAlchemy session to use

  • model – The SQLAlchemy model to query

Returns:

the object

async sqlalchemy_helpers.aio.get_or_create(session, model, **attrs)[source]

Function like Django’s get_or_create() method.

It will return a tuple, the first argument being the instance and the second being a boolean: True if the instance has been created and False otherwise.

Example: user, created = get_or_create(session, User, name="foo")