Skip to main content

Generate PlantUML ERD diagrams from SQLModel, SQLAlchemy, Django, Pydantic and dataclass models

Project description

๐Ÿ—ƒ๏ธ erdify

PyPI version Python versions License: MIT Tests Linting Ruff Checked with mypy

๐Ÿš€ Generate beautiful PlantUML Entity Relationship Diagrams from your SQLModel, SQLAlchemy, Django, Pydantic and dataclass models automatically!

erdify parses your model files using AST (Abstract Syntax Tree) and generates comprehensive ERD diagrams in PlantUML format. It supports SQLModel, SQLAlchemy 2.0, Django ORM, Pydantic and standard-library dataclasses. No database connection required!

โœจ Features

  • ๐Ÿ“Š Automatic ERD Generation - Parse your models and generate PlantUML diagrams
  • ๐Ÿงฌ 5 Frameworks - SQLModel, SQLAlchemy 2.0, Django ORM, Pydantic and dataclasses
  • ๐Ÿ” AST-Based Parsing - No imports needed, works with any valid Python code
  • ๐ŸŽฏ Zero Runtime Dependencies - Uses only Python standard library
  • ๐Ÿ”— Relationship Detection - Automatically detects foreign keys and relationships
  • ๐Ÿ”‘ Key Inference - --infer-keys derives PK/FK from field names for keyless models
  • ๐Ÿšซ Exclude Patterns - Filter out entities by class or table name with glob patterns
  • ๐ŸŽš๏ธ Source Filtering - Restrict the diagram to specific model kinds with --sources
  • โš™๏ธ Config File - Commit options under [tool.erdify] in pyproject.toml
  • โœ… Drift Check - --check fails CI/pre-commit when the committed diagram is stale
  • ๐Ÿ“ฆ Inheritance Support - Resolves fields from base classes and mixins
  • ๐Ÿท๏ธ Enum Support - Includes enum definitions in the diagram
  • ๐Ÿ”„ Link Table Detection - Identifies many-to-many association tables structurally
  • ๐ŸŽจ Beautiful Output - Clean, readable PlantUML with proper styling

๐Ÿš€ Quick Start

# Run without installing (or `pip install erdify`)
uvx erdify ./src/database -o erd.puml
# Output to stdout, with a custom title
erdify ./src/models --title "My Database Schema"

See the documentation for installation options, the full CLI, the Python API and more.

๐Ÿงฌ One Schema, Five Frameworks

The snippets below all describe the same User / Order schema โ€” only the syntax differs. Each one produces the identical diagram:

Framework comparison ERD

โ„น๏ธ The SQLModel, SQLAlchemy and Django versions declare keys explicitly (Django via its implicit id and ForeignKey). Pydantic and dataclasses have no key concept, so they are rendered with --infer-keys (id โ†’ PK, <x>_id โ†’ FK) to match. The runnable sources live in docs/examples/.

SQLModelSQLAlchemy 2.0
from sqlmodel import (
    Field, Relationship, SQLModel,
)


class User(SQLModel, table=True):
    __tablename__: str = "user"
    id: int = Field(primary_key=True)
    name: str
    email: str
    orders: list["Order"] = Relationship(
        back_populates="user")


class Order(SQLModel, table=True):
    __tablename__: str = "order"
    id: int = Field(primary_key=True)
    user_id: int = Field(
        foreign_key="user.id")
    total: float
    user: "User" = Relationship(
        back_populates="orders")
from sqlalchemy import ForeignKey
from sqlalchemy.orm import (
    DeclarativeBase, Mapped,
    mapped_column, relationship,
)


class Base(DeclarativeBase): ...


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(
        primary_key=True)
    name: Mapped[str] = mapped_column()
    email: Mapped[str] = mapped_column()
    orders: Mapped[list["Order"]] = (
        relationship(back_populates="user"))


class Order(Base):
    __tablename__ = "order"
    id: Mapped[int] = mapped_column(
        primary_key=True)
    user_id: Mapped[int] = mapped_column(
        ForeignKey("user.id"))
    total: Mapped[float] = mapped_column()
    user: Mapped["User"] = relationship(
        back_populates="orders")
Pydantic --infer-keysDataclass --infer-keys
from pydantic import BaseModel


class User(BaseModel):
    id: int
    name: str
    email: str
    orders: list["Order"] = []


class Order(BaseModel):
    id: int
    user_id: int
    total: float
    user: "User"
from dataclasses import dataclass, field


@dataclass
class User:
    id: int
    name: str
    email: str
    orders: list["Order"] = field(
        default_factory=list)


@dataclass
class Order:
    id: int
    user_id: int
    total: float
    user: "User" = None
Django ORM
from django.db import models


class User(models.Model):       # implicit `id` PK; CharField/EmailField -> str
    name = models.CharField(max_length=100)
    email = models.EmailField()

    class Meta:
        db_table = "user"


class Order(models.Model):
    user = models.ForeignKey(    # -> user_id : int foreign key column
        User, on_delete=models.CASCADE)
    total = models.FloatField()  # -> float

    class Meta:
        db_table = "order"

How each framework is detected & parsed:

Framework Detected by Keys Relationships
SQLModel table=True Field(primary_key=โ€ฆ, foreign_key=โ€ฆ) Relationship()
SQLAlchemy 2.0 __tablename__ + Mapped[...] columns mapped_column(primary_key=โ€ฆ), ForeignKey(...) relationship() (lowercase)
Django ORM models.Model subclass primary_key=True or implicit id, ForeignKey/OneToOneField ForeignKey (N:1), OneToOneField (1:1), ManyToManyField (M:N, incl. through=)
Pydantic BaseModel subclass (incl. transitive) --infer-keys only nested model refs (user: User, list["Order"])
Dataclass @dataclass decorator --infer-keys only nested model refs

โ„น๏ธ Mixins / abstract bases (e.g. a SQLAlchemy mixin without __tablename__, or a Django class Meta: abstract = True base) are not drawn as tables, but their columns are inherited into concrete entities.

For a worked example with the generated PlantUML, see the Frameworks Overview; for Django specifics see Django ORM.

๐Ÿ“š Documentation

๐Ÿ“‹ Supported Features

Feature Status Notes
Primary Keys โœ… Field(primary_key=True)
Foreign Keys โœ… Field(foreign_key="table.column")
Nullable Fields โœ… str | None or Optional[str]
Default Values โœ… Field(default=value)
Indexes โœ… Field(index=True)
Enums โœ… Python Enum classes
Relationships โœ… Relationship()
Inheritance โœ… Mixin classes supported
Link Tables โœ… Many-to-many detection
Custom Table Names โœ… __tablename__ attribute
Exclude Patterns โœ… --exclude glob on class/table name
Key Inference โœ… --infer-keys for Pydantic/dataclass (id, <x>_id)
SQLModel โœ… Field() / Relationship()
SQLAlchemy 2.0 โœ… Mapped[...] / mapped_column()
Pydantic โœ… BaseModel subclasses, nested refs as relationships
Dataclass โœ… @dataclass, nested refs as relationships

๐Ÿ—บ๏ธ Roadmap

Recently shipped:

Feature Status Description
Exclude Option โœ… Done Exclude tables or entities from ERD generation using glob patterns
SQLAlchemy Support โœ… Done Native support for SQLAlchemy 2.0 (Mapped / mapped_column) models
Pydantic Support โœ… Done Generate ERDs from Pydantic models, with optional --infer-keys
Dataclass Support โœ… Done Support for standard Python dataclasses with type annotations

Have a feature request? Please open an issue on GitHub to discuss it!

๐Ÿค Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

๐Ÿ”’ Security

For security concerns, please see SECURITY.md.

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

erdify stands on the shoulders of great open-source projects:

Supported model frameworks

  • SQLModel - The awesome SQL database library
  • SQLAlchemy - The Python SQL toolkit and ORM
  • Django - The web framework whose ORM models erdify parses
  • Pydantic - Data validation using Python type hints
  • dataclasses - Python standard-library data classes

Rendering & output

  • PlantUML - For the diagram rendering
  • Graphviz - Layout engine behind PlantUML's ER diagrams

Tooling & infrastructure

  • uv - Packaging, builds and dependency management
  • Ruff - Linting and formatting
  • mypy - Static type checking
  • pytest - Testing framework
  • pre-commit - Git hook management

Community


Made with โค๏ธ by Devsuit GmbH

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

erdify-0.6.0rc1.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

erdify-0.6.0rc1-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file erdify-0.6.0rc1.tar.gz.

File metadata

  • Download URL: erdify-0.6.0rc1.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for erdify-0.6.0rc1.tar.gz
Algorithm Hash digest
SHA256 57f80e6a5f80cdc25cf05ed23d4e42a9d1ffa9eba3cebb0f57c6177c2500eb81
MD5 2e5cc1ae9260aebe48cb508c7b99213f
BLAKE2b-256 564e0a50f2e2dfc2329306a6be02b30844c323302d13fdff72591d928683f650

See more details on using hashes here.

File details

Details for the file erdify-0.6.0rc1-py3-none-any.whl.

File metadata

  • Download URL: erdify-0.6.0rc1-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for erdify-0.6.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 8336f2a332b834e44e1ddcc8cc31db362323b3f8a3610d2e929ca3495c850390
MD5 99995f68ef70b873b712c1f5eb741d4b
BLAKE2b-256 e1315f2c4061ec77efe28f95567d525231a6af372117bceb0763198811426377

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