A library to generate Pydantic models from SQLAlchemy models.
Project description
SQLAlchemy-Pydantic Codegen
A Python library for generating Pydantic models from SQLAlchemy models, providing a seamless integration between SQLAlchemy and Pydantic for data validation and serialization.
Features
- Automatic Pydantic model generation from SQLAlchemy models.
- Relationship support: Nested models for SQLAlchemy relationships.
- Enum reflection: SQLAlchemy enums become Pydantic enums.
- Field metadata: Comments become descriptions, string lengths become
max_length. - Jinja2 templating for customizable output.
- CLI for easy usage, including output directory and config options.
- Custom JSON/JSONB field mapping to your own Pydantic models.
- Post-generation cleaning to remove or replace fields as needed.
- Auto-generated
__init__.pyfor schema packages. - Ready for formatting tools (e.g., Black, Ruff).
Installation
uv add sqlalchemy-pydantic-codegen
or
pip install sqlalchemy-pydantic-codegen
Preparing Your SQLAlchemy Models
Before using sqlalchemy-pydantic-codegen, you may want to generate your SQLAlchemy models from an existing database using sqlacodegen. This tool introspects your database and creates SQLAlchemy model classes automatically. To generate models, run:
sqlacodegen postgresql://user:password@localhost/dbname --outfile src/db/models.py
Once your models are generated, you can proceed to use sqlalchemy-pydantic-codegen as described below.
Usage
After installation, use the CLI to generate Pydantic models from your SQLAlchemy models.
Basic Usage
sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas
--models-path: Dotted path to your SQLAlchemy models module (required).
--output-dir: Directory for generated Pydantic 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.
Advanced
- Supports nested relationships and cyclic reference handling.
- Enum columns become Pydantic enums.
- Field comments and constraints are preserved as Pydantic metadata.
- Easily extend templates for your own conventions.
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
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 sqlalchemy_pydantic_codegen-0.1.0.tar.gz.
File metadata
- Download URL: sqlalchemy_pydantic_codegen-0.1.0.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29dd1cbeb5fd2853693bb8c3af0982f36b28197aeb65ea2bd9cb894ebbc7963b
|
|
| MD5 |
a97a50355a5aab76ad858bd8cf41402d
|
|
| BLAKE2b-256 |
c22104054deeeda948bc8762df96576aa3e6d8b5f251b611ec5c155d165c5424
|
File details
Details for the file sqlalchemy_pydantic_codegen-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_pydantic_codegen-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8070dcfb1c914e9acc8f9c4664b3f4d6f3fd8d99c5dbb5881f52948a960adb59
|
|
| MD5 |
870320945686e400816cbb0db9d36e22
|
|
| BLAKE2b-256 |
97d9c9d5e8abdaa5b53f79230e3409e91a5be9a89d53eaafc834fb469fd82c56
|