Skip to main content

Define a single model that works as both Pydantic and SQLAlchemy, with explicit conversion between the two

Project description

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

Project details


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.3.4.tar.gz (146.4 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.3.4-py3-none-any.whl (46.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlcrucible-0.3.4.tar.gz
  • Upload date:
  • Size: 146.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcrucible-0.3.4.tar.gz
Algorithm Hash digest
SHA256 aff10a6efba8a7a50057039b8d1b98e17b5063c1fe11c635c8ae7d33da1c20d1
MD5 7734a49ca0827a44d54e8c9839270977
BLAKE2b-256 9d04c7c74e0e2e6541acb6b1fedf14396be088fa3998ca9c0978c6bfeb7f99c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcrucible-0.3.4.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.3.4-py3-none-any.whl.

File metadata

  • Download URL: sqlcrucible-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 46.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlcrucible-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e4519476482d95b0ecf7a0d76e9e40724358d6e39c8e2263ade6695847bf3684
MD5 d85fb5015cfadccdb4fd0d7bf59e2a14
BLAKE2b-256 1f9fc4de3575faf1d5e811e58a037620d98b80807448b56c1858228d1783f24c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlcrucible-0.3.4-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.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page