Reference
Core Module
The core functionality of sqlalchemy-tenants.
get_table_policy
Returns the SQL policy for a given table name.
Source code in src/sqlalchemy_tenants/core.py
get_tenant_role_name
Get the Postgres role name for the given tenant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
the tenant slug. |
required |
Returns:
Type | Description |
---|---|
str
|
The Postgres role name for the tenant. |
Source code in src/sqlalchemy_tenants/core.py
with_rls
Decorator to apply RLS (Row Level Security) to a SQLAlchemy model. Validates that the model includes a 'tenant' column.
Source code in src/sqlalchemy_tenants/core.py
Managers
sqlalchemy_tenants.managers.DBManager
Bases: Protocol
create_tenant
abstractmethod
Create a new tenant with the specified identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
The identifier (slug or ID) of the tenant to create. |
required |
delete_tenant
abstractmethod
Delete a tenant and all its associated roles and privileges, reassigning owned objects to the current user.
No data will be deleted, only the role and privileges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
The identifier of the tenant to delete. |
required |
Source code in src/sqlalchemy_tenants/managers.py
list_tenants
abstractmethod
Get all the available tenants.
Returns:
Type | Description |
---|---|
Set[TenantIdentifier]
|
A set with all the available tenants. |
new_tenant_session
abstractmethod
new_tenant_session(
tenant: TenantIdentifier, create_if_missing: bool = True
) -> ContextManager[TenantSession]
Create a new SQLAlchemy session scoped to a specific tenant.
The session uses the tenant's PostgreSQL role and is subject to Row-Level Security (RLS) policies. All queries and writes are automatically restricted to data belonging to the specified tenant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
The identifier of the tenant. |
required |
create_if_missing
|
bool
|
Whether to create the tenant role if it doesn't exist. |
True
|
Yields:
Type | Description |
---|---|
ContextManager[TenantSession]
|
A SQLAlchemy session restricted to the tenant's data via RLS. |
Raises:
Type | Description |
---|---|
TenantNotFound
|
If the tenant role doesn't exist and |
Source code in src/sqlalchemy_tenants/managers.py
new_session
abstractmethod
Create a new admin session with unrestricted access to all tenant data.
This session is not bound to any tenant role and is not subject to RLS policies.
Yields:
Type | Description |
---|---|
ContextManager[Session]
|
An asynchronous SQLAlchemy session with full database access. |
Source code in src/sqlalchemy_tenants/managers.py
sqlalchemy_tenants.managers.PostgresManager
Managers [async]
sqlalchemy_tenants.aio.managers.DBManager
Bases: Protocol
create_tenant
abstractmethod
async
Create a new tenant with the specified identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
The identifier of the tenant to create. |
required |
delete_tenant
abstractmethod
async
Delete a tenant and all its associated roles and privileges, reassigning owned objects to the current user.
No data will be deleted, only the role and privileges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
The identifier of the tenant to delete. |
required |
Source code in src/sqlalchemy_tenants/aio/managers.py
list_tenants
abstractmethod
async
Get all the available tenants.
Returns:
Type | Description |
---|---|
Set[TenantIdentifier]
|
A set with all the available tenants. |
new_tenant_session
abstractmethod
new_tenant_session(
tenant: TenantIdentifier, create_if_missing: bool = True
) -> AsyncContextManager[AsyncTenantSession]
Create a new SQLAlchemy session scoped to a specific tenant.
The session uses the tenant's PostgreSQL role and is subject to Row-Level Security (RLS) policies. All queries and writes are automatically restricted to data belonging to the specified tenant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tenant
|
TenantIdentifier
|
The tenant identifier, which must match a valid PostgreSQL role used for RLS enforcement. |
required |
create_if_missing
|
bool
|
Whether to create the tenant role if it doesn't exist. |
True
|
Yields:
Type | Description |
---|---|
AsyncContextManager[AsyncTenantSession]
|
A SQLAlchemy session restricted to the tenant's data via RLS. |
Raises:
Type | Description |
---|---|
TenantNotFound
|
If the tenant role doesn't exist and |
Source code in src/sqlalchemy_tenants/aio/managers.py
new_session
abstractmethod
Create a new admin session with unrestricted access to all tenant data.
This session is not bound to any tenant role and is not subject to RLS policies.
Yields:
Type | Description |
---|---|
AsyncContextManager[AsyncSession]
|
An asynchronous SQLAlchemy session with full database access. |
Source code in src/sqlalchemy_tenants/aio/managers.py
sqlalchemy_tenants.aio.managers.PostgresManager
PostgresManager(
schema_name: str,
engine: AsyncEngine,
session_maker: async_sessionmaker[AsyncSession],
)
Exceptions
Exception classes used throughout the library.
SqlalchemyTenantErr
Bases: Exception
Base class for all exceptions raised by the tenants package.
TenantAlreadyExists
Bases: SqlalchemyTenantErr
Raised when trying to create a tenant that already exists.
Source code in src/sqlalchemy_tenants/exceptions.py
TenantNotFound
Bases: SqlalchemyTenantErr
Raised when a tenant is not found.