Pyramid SQLAlchemy Integration
Project description
pyramid-sa
Pyramid SQLAlchemy Integration — a reusable library that wires SQLAlchemy into any Pyramid application.
Features
- Session management — engine, transaction-managed sessions,
request.dbsession - Declarative base —
Basewith naming conventions and utility methods (as_dict,copy_with) - Audit trail — opt-in
AuditMixintracking created/updated by whom and when - Soft delete — opt-in
SoftDeleteMixinwith query filtering, unique indexes, and safe restore - Error mapping —
NoResultFound→ 404,IntegrityError→ 409, with customizable bodies - JSON rendering —
datetime,date,UUIDadapters out of the box - Alembic scaffold —
db init-alembicfor pre-wired migration setup - CLI commands —
db drop,db initializefor schema management - Test fixtures — companion
pyramid-sa-testingpackage with PostgreSQL-backed pytest plugin
Documentation
Full documentation is available at the docs site, covering all features in detail.
Installation
pip install pyramid-sa
For test fixtures (dev only):
pip install pyramid-sa-testing
Quick start
In your Pyramid app factory:
from pyramid.config import Configurator
def create_app(global_config=None, dbengine=None, **settings):
config = Configurator(settings=settings)
if dbengine is not None:
config.registry["dbengine"] = dbengine
config.include("pyramid_sa")
config.sa_enable_audit() # optional: auto-populate audit fields
config.sa_enable_soft_delete() # optional: soft-delete behavior
config.sa_scan_models("myapp.models")
config.scan(".views")
return config.make_wsgi_app()
What config.include("pyramid_sa") does
- Includes
pyramid_tm(transaction management) - Creates the SQLAlchemy engine from
sqlalchemy.urlin settings (or usesconfig.registry["dbengine"]if pre-set) - Registers a session factory and adds
request.dbsessionas a reified property - Adds an exception tween (
NoResultFound→ 404,IntegrityError→ 409) - Configures a JSON renderer with adapters for
datetime,date, andUUID - Registers directives:
sa_scan_models,sa_enable_audit,sa_enable_soft_delete,sa_error_formatter
Base vs Model
Use Base for plain models. Use Model for models that need both audit columns and soft-delete:
from pyramid_sa import Base, Model, generate_uuid
class Item(Base):
"""Plain model — no audit or soft-delete columns."""
__tablename__ = "items"
id: Mapped[int] = mapped_column(primary_key=True)
uuid: Mapped[uuid.UUID] = mapped_column(default=generate_uuid, unique=True)
name: Mapped[str] = mapped_column(String(255))
class Article(Model):
"""Audited + soft-deletable model."""
__tablename__ = "articles"
id: Mapped[int] = mapped_column(primary_key=True)
title: Mapped[str] = mapped_column(String(255))
See the Models docs for as_dict(), copy_with(), and selective mixin usage.
Alembic
Scaffold alembic in your project:
db init-alembic
Then edit alembic/env.py to import your models:
from alembic import context
from pyramid_sa import Base
from pyramid_sa.scripts.alembic import run_migrations_offline, run_migrations_online
import myapp.models # noqa: F401
target_metadata = Base.metadata
if context.is_offline_mode():
run_migrations_offline(target_metadata)
else:
run_migrations_online(target_metadata)
CLI
Compose the db commands into your app's CLI:
import click
from pyramid_sa.scripts.cli import db
@click.group()
@click.option("--config-uri", default="development.ini", show_default=True)
@click.pass_context
def cli(ctx, config_uri):
ctx.ensure_object(dict)
ctx.obj["config_uri"] = config_uri
cli.add_command(db)
Commands: db init-alembic, db drop, db initialize (--drop-before, --run-thru-alembic).
Test Fixtures
Install pyramid-sa-testing and the pytest plugin is auto-discovered:
| Fixture | Scope | Description |
|---|---|---|
pyramid_sa_engine |
session | PostgreSQL engine via pytest-postgresql |
pyramid_sa_tm |
function | Doomed transaction manager — auto-rollback after each test |
pyramid_sa_dbsession |
function | DB session bound to the test transaction |
pyramid_sa_testapp |
function | WebTest TestApp with session injected |
pyramid_sa_app_request |
function | Real Pyramid request for service-layer testing |
Your conftest.py must provide an app fixture:
import pytest
from pyramid_sa import Base
@pytest.fixture(scope="session")
def app(pyramid_sa_engine):
from myapp.app import create_app
wsgi_app = create_app(dbengine=pyramid_sa_engine)
Base.metadata.create_all(pyramid_sa_engine)
return wsgi_app
See the Testing docs for examples and advanced usage.
Development
uv sync --dev
uv run pytest
uv run ruff check .
uv run black .
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyramid_sa-0.7.1.tar.gz.
File metadata
- Download URL: pyramid_sa-0.7.1.tar.gz
- Upload date:
- Size: 94.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f80d044edad423b23e202eaf4afd10af22f6037576267559917b50bd25703b2
|
|
| MD5 |
5ce59f0cbacaa00b151e9161642d73a5
|
|
| BLAKE2b-256 |
2bef1aa1d7a8458fe4d3da8907265c9b9a260190c0f511a8e82727bc75982c45
|
Provenance
The following attestation bundles were made for pyramid_sa-0.7.1.tar.gz:
Publisher:
ci.yml on tomascorrea/pyramid-sa
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyramid_sa-0.7.1.tar.gz -
Subject digest:
8f80d044edad423b23e202eaf4afd10af22f6037576267559917b50bd25703b2 - Sigstore transparency entry: 1155463496
- Sigstore integration time:
-
Permalink:
tomascorrea/pyramid-sa@65598eae6d7a6565953371fe546cf976324ef486 -
Branch / Tag:
refs/tags/v0.7.1 - Owner: https://github.com/tomascorrea
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@65598eae6d7a6565953371fe546cf976324ef486 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyramid_sa-0.7.1-py3-none-any.whl.
File metadata
- Download URL: pyramid_sa-0.7.1-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5ee8be225a93edf5ba28baa7b69dccc90fce61362bfc17c78228b527eeba3e5
|
|
| MD5 |
1970c499de517d392871e527b1db0281
|
|
| BLAKE2b-256 |
4d620724dc27d566affdd7c4d84f6b24d03fdfcc2c19298566d2549d1d4969d3
|
Provenance
The following attestation bundles were made for pyramid_sa-0.7.1-py3-none-any.whl:
Publisher:
ci.yml on tomascorrea/pyramid-sa
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyramid_sa-0.7.1-py3-none-any.whl -
Subject digest:
e5ee8be225a93edf5ba28baa7b69dccc90fce61362bfc17c78228b527eeba3e5 - Sigstore transparency entry: 1155463502
- Sigstore integration time:
-
Permalink:
tomascorrea/pyramid-sa@65598eae6d7a6565953371fe546cf976324ef486 -
Branch / Tag:
refs/tags/v0.7.1 - Owner: https://github.com/tomascorrea
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@65598eae6d7a6565953371fe546cf976324ef486 -
Trigger Event:
release
-
Statement type: