Skip to main content

SQLCrucible

Get the full power of Pydantic for your API and the full power of SQLAlchemy for your database, without compromising on either.

SQLCrucible lets you define a single model class that works as both a Pydantic model and a SQLAlchemy table, with explicit conversion between the two. Your Pydantic models stay pure Pydantic (working with FastAPI, validation, serialization), your SQLAlchemy models stay pure SQLAlchemy (supporting relationships, inheritance, Alembic), and conversion only happens when you ask for it.

Key Features

  • Explicit conversionto_sa_model() and from_sa_model() make it clear when you're crossing the boundary between Pydantic and SQLAlchemy
  • Native SQLAlchemy — use mapped_column(), relationship(), and __mapper_args__ directly; if SQLAlchemy supports it, so does SQLCrucible
  • Escape hatches everywhere — mix with plain SQLAlchemy models, attach entities to existing tables, customize generated models, or drop down to raw queries
  • Full inheritance support — single table, joined table, and concrete table inheritance all work out of the box
  • Type stub generation — generate .pyi stubs for IDE autocompletion and type checking of SQLAlchemy columns
  • Multiple frameworks — works with Pydantic, dataclasses, and attrs

Example

from typing import Annotated
from uuid import UUID, uuid4

from pydantic import Field
from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session, mapped_column

from sqlcrucible import SAType, SQLCrucibleBaseModel

class Artist(SQLCrucibleBaseModel):
    __sqlalchemy_params__ = {"__tablename__": "artist"}

    id: Annotated[UUID, mapped_column(primary_key=True)] = Field(default_factory=uuid4)
    name: str

engine = create_engine("sqlite:///:memory:")
SAType[Artist].__table__.metadata.create_all(engine)

artist = Artist(name="Bob Dylan")
with Session(engine) as session:
    session.add(artist.to_sa_model())
    session.commit()

with Session(engine) as session:
    sa_artist = session.scalar(
        select(SAType[Artist]).where(SAType[Artist].name == "Bob Dylan")
    )
    artist = Artist.from_sa_model(sa_artist)

Installation

pip install sqlcrucible

Documentation

Full documentation is available at sqlcrucible.rdrj.uk.


🏴󠁧󠁢󠁳󠁣󠁴󠁿 Made with haggis

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlcrucible-0.5.1.tar.gz (166.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlcrucible-0.5.1-py3-none-any.whl (56.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlcrucible-0.5.1.tar.gz.

File metadata

  • Download URL: sqlcrucible-0.5.1.tar.gz
  • Upload date:
  • Size: 166.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sqlcrucible-0.5.1.tar.gz
Algorithm Hash digest
SHA256 66df805774fc31a4e3cade65a760eec9926ee1d0cef15852db31e1d8d3158a1b
MD5 b1c466c5748ea7aeadb220ad4cf11776
BLAKE2b-256 f82cca75d61c8eef5d089c79341b03e8fcbdc0117f79ca8fb776c3dea21ba014

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcrucible-0.5.1.tar.gz:

Publisher: ci.yml on RichardDRJ/sqlcrucible

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlcrucible-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: sqlcrucible-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 56.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sqlcrucible-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 68d8626c89fe4113ec55ce853199baf84dcd15d748074da0a610d1cee7ed177d
MD5 492a2b3e5bccfcf3ad4134d81e39f37b
BLAKE2b-256 bdcfab23c5834cd584faa0e9095fab898cf004b3a6496f688f2ec4a70caba4f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcrucible-0.5.1-py3-none-any.whl:

Publisher: ci.yml on RichardDRJ/sqlcrucible

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.6

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page