Skip to main content

A library to generate Pydantic models from SQLAlchemy models.

Project description

🐍🔗 SQLAlchemy-Pydantic Codegen

License PyPI Python Unit Tests

A Python library for generating Pydantic models from SQLAlchemy models, providing a seamless integration between SQLAlchemy and Pydantic for data validation and serialization.

✨ Key Features

  • Automatic Pydantic model generation from SQLAlchemy models.
  • Relationship support: Nested models for SQLAlchemy relationships.
  • Custom JSON/JSONB field mapping to your own Pydantic models.
  • Auto-generated __init__.py for schema packages.

📦 Installation

uv add sqlalchemy-pydantic-codegen

🚀 Usage

We recommend sqlacodegen to generate your SQLAlchemy models automatically.

Once your SQLAlchemy models are ready, generate Pydantic models with:

sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas
  • --models-path: Dotted path to your SQLAlchemy models (required)
  • --output-dir: Output directory for generated schemas (default: src/schemas)

🛠️ Custom Configuration

To map JSON/JSONB fields to custom Pydantic models, use the --config option.

Create a config file (e.g., codegen_config.py):

# codegen_config.py

# Maps table names to a dictionary of field names and the Pydantic model to use.
CUSTOM_JSONB_MODELS = {
    "my_table": {
        "my_jsonb_field": "MyCustomPydanticModelForJsonbField",
    },
}

# Maps the Pydantic model name to its full import statement.
CUSTOM_IMPORTS = {
    "MyCustomPydanticModelForJsonbField": "from my_app.schemas import MyCustomPydanticModelForJsonbField",
}

Then, run the command with the --config flag:

sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas --config codegen_config.py

📤 Output

  • One Pydantic schema file per SQLAlchemy model.
  • init.py with all exports and forward references.
  • Cleaned and ready-to-use Pydantic models.

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

sqlalchemy_pydantic_codegen-1.1.0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_pydantic_codegen-1.1.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_pydantic_codegen-1.1.0.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_pydantic_codegen-1.1.0.tar.gz
Algorithm Hash digest
SHA256 cfeee4c3f1c63a3cdc55ad68177ad3754e4d367c8e0d56bf17275aa8b76f6f21
MD5 ce3c04143ae34af05a61b7c10ff1f678
BLAKE2b-256 22812ddea88dc508c46286df6b32061c80c38f7cbf9edfc74a631a9735942152

See more details on using hashes here.

File details

Details for the file sqlalchemy_pydantic_codegen-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_pydantic_codegen-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8548814ecfae44ce9e48a130ba4810b6b07592e69a3d2e88b19aa50cc6f62d2f
MD5 3b2175fe2c30dd8fb012fcfcbec89336
BLAKE2b-256 9764dd2c37c6b8894f17643cc52c29d023240fb3d204e11d3249044a9203bc60

See more details on using hashes here.

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