Skip to main content

CLI tool to generate SQLModel models from a Postgres database via SQLAlchemy introspection.

Project description

SQLModelGenerator

MIT License PyPI

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:

  1. Fork the repository and create your branch from main.
  2. Install dependencies:
    python -m venv .venv
    .venv/Scripts/activate  # On Windows
    # Or: source .venv/bin/activate  # On macOS/Linux
    pip install -e .[dev]
    
  3. Write tests for your changes (see tests/ directory).
  4. Lint and format your code:
    black src/ tests/
    flake8 src/ tests/
    mypy src/ tests/
    
  5. Open a pull request with a clear description of your changes.

Code style:


Community & Support

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


Download files

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

Source Distribution

sqlmodelgenerator-0.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

sqlmodelgenerator-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

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

Hashes for sqlmodelgenerator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a854400228a36b1d53654c31c289d04bcc1b12ab5bff79cf5c61f34173cc4633
MD5 ab89572a83067deaef51aca41b4c308d
BLAKE2b-256 a6bb181ebba63978478a584459a312e644a5a16e69dfb45ff41deaabdee543a9

See more details on using hashes here.

File details

Details for the file sqlmodelgenerator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlmodelgenerator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a60c94b1284f527c17bb4724a717d6e697cd7a2b7d21b5572a37eac61c394b0c
MD5 7691ac92fdd21049102da5b879fb83d8
BLAKE2b-256 1bd3a31155d7b7d5c3aa4f04fc10b8f3e17bf2a42b7c0d0d49b3cd8c9ac952db

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