CLI tool to generate SQLModel models from a Postgres database via SQLAlchemy introspection.
Project description
SQLModelGenerator
SQLModelGenerator is a CLI tool that automatically generates SQLModel models (including enums) directly from a Postgres database via SQLAlchemy introspection.
Features
- 🚀 Automatic model & enum generation from your live Postgres database
- ⚙️ Configurable: exclusions, naming rules, type overrides, and more
- 🗄️ Postgres support: arrays, JSONB, enums, relationships, and more
- 🔗 Relationship inference: detects and generates foreign key relationships
- 🖥️ CLI interface: built with Typer
- 👐 Open-source & commercial friendly: MIT license, use for any purpose
Installation
From PyPI (recommended):
pip install sqlmodelgenerator
For local development:
python -m venv .venv
.venv/Scripts/activate # On Windows
# Or: source .venv/bin/activate # On macOS/Linux
pip install -e .[dev]
Usage
Place a config file named sqlmodelgenerator.yaml, sqlmodelgenerator.yml, or sqlmodelgenerator.toml in your project root, or specify one with --config.
# With default config file in current directory
sqlmodelgenerator generate
sqlmodelgenerator inspect
# Or specify a config file
sqlmodelgenerator generate --config path/to/config.yaml
sqlmodelgenerator inspect --config path/to/config.yaml
Configuration
Supported config formats: YAML or TOML. Example:
database_url: postgresql://user:pass@localhost:5432/mydb
output_dir: models
enum_output_path: enums
exclude_tables: [alembic_version]
exclude_columns: []
field_type_overrides: {}
relationship_mode: full
cleanup_old_files: true
See example.sqlmodelgenerator.yaml or example.sqlmodelgenerator.toml for all options.
Example Output
# models/user.py
from sqlmodel import SQLModel, Field, Relationship
from enums.user_status import UserStatus
from typing import Optional
class User(SQLModel, table=True):
id: int = Field(primary_key=True)
name: str
status: UserStatus
profile_id: Optional[int] = Field(default=None, foreign_key="profile.id")
profile: Optional[Profile] = Relationship(back_populates="user")
Development & Testing
- All tests are in the
tests/directory. - Run tests:
pytest - Coverage:
pytest --cov=src/sqlmodelgenerator - Linting:
flake8 src/ tests/ - Type checking:
mypy src/ tests/ - Formatting:
black src/ tests/
Contributing
We welcome contributions of all kinds! To get started:
- Fork the repository and create your branch from
main. - Install dependencies:
python -m venv .venv .venv/Scripts/activate # On Windows # Or: source .venv/bin/activate # On macOS/Linux pip install -e .[dev]
- Write tests for your changes (see
tests/directory). - Lint and format your code:
black src/ tests/ flake8 src/ tests/ mypy src/ tests/
- Open a pull request with a clear description of your changes.
Code style:
Community & Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Contact: opensource@finatic.dev
If you have questions, ideas, or need help, open an issue or start a discussion!
License
MIT License — free for personal, open-source, or commercial use. See LICENSE.
Developed by Finatic.dev — Contributions welcome!
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 sqlmodelgenerator-0.1.0.tar.gz.
File metadata
- Download URL: sqlmodelgenerator-0.1.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a854400228a36b1d53654c31c289d04bcc1b12ab5bff79cf5c61f34173cc4633
|
|
| MD5 |
ab89572a83067deaef51aca41b4c308d
|
|
| BLAKE2b-256 |
a6bb181ebba63978478a584459a312e644a5a16e69dfb45ff41deaabdee543a9
|
File details
Details for the file sqlmodelgenerator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlmodelgenerator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a60c94b1284f527c17bb4724a717d6e697cd7a2b7d21b5572a37eac61c394b0c
|
|
| MD5 |
7691ac92fdd21049102da5b879fb83d8
|
|
| BLAKE2b-256 |
1bd3a31155d7b7d5c3aa4f04fc10b8f3e17bf2a42b7c0d0d49b3cd8c9ac952db
|