Generate PlantUML ERD diagrams from SQLModel, SQLAlchemy, Django, Pydantic and dataclass models
Project description
๐๏ธ erdify
๐ 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 (
Mapped[...]/mapped_column()), 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 - Optional
--infer-keysderives 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(e.g. ORM tables only) - ๐ฆ Inheritance Support - Correctly 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 (two FK columns that form the primary key), regardless of class name, including SQLAlchemy Core
Table()association tables referenced viarelationship(secondary=...) - ๐จ Beautiful Output - Clean, readable PlantUML with proper styling
๐ฆ Installation
Using pip
pip install erdify
Using uv
uv add erdify
Using pipx (recommended for CLI usage)
pipx install erdify
Using uvx (no installation needed)
# Run directly without installing
uvx erdify ./src/database -o erd.puml
๐ Quick Start
Command Line
# Generate ERD from a models directory
erdify ./src/database -o erd.puml
# With custom title
erdify ./src/models --title "My Database Schema" -o schema.puml
# Output to stdout
erdify ./src/database
Python API
from pathlib import Path
from erdify import parse_models_directory, generate_plantuml
# Parse your models
entities, enums = parse_models_directory(Path("./src/database"))
# Generate PlantUML
diagram = generate_plantuml(
entities=entities,
enums=enums,
title="My Database ERD"
)
# Save or use the diagram
Path("erd.puml").write_text(diagram)
๐ Usage
CLI Options
usage: erdify [-h] [-o OUTPUT] [--title TITLE] [--exclude [PATTERN ...]]
[--sources [KIND ...]] [--infer-keys] [--django-raw-types]
[--no-enums] [--no-relationships] [-v]
input
Generate PlantUML ERD diagrams from SQLModel, SQLAlchemy, Django, Pydantic and dataclass models
positional arguments:
input Directory containing model files (searches for models.py recursively)
options:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
Output .puml file (default: stdout)
--title TITLE Diagram title (default: 'Database ERD')
--exclude [PATTERN ...]
Glob patterns (case-sensitive) to exclude entities by
class name or table name, e.g. --exclude '*Link' audit_log
--sources [KIND ...] Restrict which model kinds become entities. Choices:
sqlmodel, sqlalchemy, django, dataclass, pydantic.
Default: all, e.g. --sources sqlmodel sqlalchemy for DB
tables only
--infer-keys For keyless models (Pydantic/dataclass), infer a primary
key from a field named 'id' and a foreign key from '<x>_id'
--django-raw-types For Django models, show original field names (CharField,
TextField) instead of mapped Python types (str, int)
--no-enums Skip enum definitions in output
--no-relationships Skip relationship lines in output
-v, --version show program's version number and exit
Excluding Entities
Use --exclude to drop tables/entities from the diagram. Each pattern is a
case-sensitive glob tested
against both the class name and the table name โ an entity is excluded
if either matches. Any relationships pointing at an excluded entity are dropped
too, so no dangling lines remain.
# Exclude all link tables (class names ending in "Link")
erdify ./src/database --exclude '*Link'
# Exclude by table name, with multiple patterns
erdify ./src/database --exclude audit_log 'tmp_*' Session
๐ก Quote patterns containing
*so your shell doesn't expand them.
Filtering by Model Kind
Use --sources to restrict the diagram to specific model frameworks. By default
all recognized kinds are drawn (sqlmodel, sqlalchemy, django, dataclass,
pydantic). This is the precise alternative to --exclude when you want a pure
DB-table ERD and don't want Pydantic DTOs or @dataclass query wrappers to leak in.
# Only real DB tables โ drops Pydantic/dataclass models entirely
erdify ./src/database --sources sqlmodel sqlalchemy
# ORM tables plus your Pydantic schemas, but no dataclasses
erdify ./src/database --sources sqlmodel pydantic
๐ก
--sourcesfilters by kind;--excludefilters by name. Combine them freely.
Running as Module
python -m erdify ./src/database -o erd.puml
Example Models
Given these SQLModel definitions:
from enum import Enum
from sqlmodel import SQLModel, Field, Relationship
class UserRole(Enum):
ADMIN = "admin"
USER = "user"
class User(SQLModel, table=True):
__tablename__: str = "user"
id: int = Field(primary_key=True)
name: str
email: str = Field(index=True)
role: UserRole = Field(default=UserRole.USER)
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")
The tool generates:
with following code:
@startuml Database ERD
!define primary_key(x) <b><color:#b8861b><&key></color> x</b>
!define foreign_key(x) <color:#aaaaaa><&key></color> x
!define column(x) <color:#efefef><&media-record></color> x
skinparam linetype ortho
' Enums
enum UserRole << (E,#FFCC00) >> {
ADMIN
USER
}
' Entities
entity "user" as User {
primary_key(id) : int
column(name) : str
column(email) : str
column(role) : UserRole = USER
}
entity "order" as Order {
primary_key(id) : int
foreign_key(user_id) : int
column(total) : float
}
' Relationships
Order }o--|| User : "user_id"
@enduml
๐งฌ One Schema, Five Frameworks
erdify supports five model frameworks. The snippets below all describe the
same User / Order schema โ only the syntax differs. Each one produces the
identical diagram:
โน๏ธ The SQLModel, SQLAlchemy and Django versions declare keys explicitly (Django via its implicit
idandForeignKey). 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 indocs/examples/.
| SQLModel | SQLAlchemy 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-keys | Dataclass --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 Djangoclass Meta: abstract = Truebase) are not drawn as tables, but their columns are inherited into concrete entities. Imports aliased to other names (e.g.mapped_column as mc) are not detected.
Django ORM
erdify parses Django models from source โ no Django runtime, settings, or app
registry required. A models.Model subclass becomes an entity; abstract bases
(class Meta: abstract = True) are inherited but not drawn, and a class Meta: db_table = "..." overrides the table name.
from django.db import models
class Author(models.Model): # implicit `id` primary key (int)
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE) # N:1
tags = models.ManyToManyField("Tag") # M:N
profile = models.OneToOneField("Profile", on_delete=models.CASCADE) # 1:1
class Meta:
db_table = "catalog_book"
Relationship targets are resolved by class name, including "self" and
"app.Model" string references. A ManyToManyField(through=LinkModel) is drawn
through the link model's own foreign keys (no spurious direct edge), exactly like
SQLAlchemy secondary=.
By default Django field types are mapped to readable Python types
(CharField โ str, IntegerField/AutoField โ int, DateTimeField โ
datetime, โฆ) so mixed-source diagrams stay consistent. Ambiguous or unknown
fields (JSONField, FileField, custom/third-party fields) keep their Django
name rather than fake a type. Pass --django-raw-types to show the original
Django field names everywhere instead.
Inferring keys (--infer-keys)
Pydantic models and dataclasses have no database key concept. By default all
fields are rendered as plain columns and relationships come only from nested
model references. If your models follow a database-like naming convention, pass
--infer-keys to derive keys from field names:
- a field named
idโ primary key - a field named
<x>_idโ foreign key targeting table<x>
# Plain columns (default)
erdify ./src/schemas
# Infer PK/FK from id / <x>_id naming
erdify ./src/schemas --infer-keys
โน๏ธ
--infer-keysonly affects Pydantic/dataclass models. SQLModel and SQLAlchemy keys are always read from the explicit definitions and never overridden.
๐จ Viewing the Diagram
Online
- Copy the generated
.pumlcontent - Paste at PlantUML Web Server
Local with PlantUML
# Install PlantUML (macOS)
brew install plantuml
# Generate PNG
plantuml erd.puml
# Generate SVG
plantuml -tsvg erd.puml
VS Code Extension
Install the PlantUML extension for live preview.
๐ง Advanced Usage
Programmatic Access
from erdify import (
ASTDatabaseParser,
PlantUMLGenerator,
EntityInfo,
FieldInfo,
EnumInfo,
)
# Low-level parser access
parser = ASTDatabaseParser(Path("./models"))
entities, enums = parser.parse_all_models()
# Access entity details
for name, entity in entities.items():
print(f"Table: {entity.table_name}")
for field in entity.fields:
if field.is_primary_key:
print(f" PK: {field.name}")
elif field.is_foreign_key:
print(f" FK: {field.name} -> {field.foreign_table}")
# Custom generator with options
generator = PlantUMLGenerator(
entities=entities,
enums=enums,
title="Custom ERD"
)
output = generator.generate()
Integration with CI/CD
# .github/workflows/docs.yml
name: Generate ERD
on:
push:
paths:
- 'src/database/**'
jobs:
generate-erd:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install erdify
run: pip install erdify
- name: Generate ERD
run: erdify ./src/database --title "Database Schema" -o docs/erd.puml
- name: Generate PNG
run: |
sudo apt-get install -y plantuml
plantuml docs/erd.puml
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "docs: update ERD diagram"
file_pattern: "docs/erd.*"
Integration with pre-commit hooks
Keep your ERD diagrams automatically updated on every commit using pre-commit:
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: generate-erd
name: ๐๏ธ Generate ERD Diagram
entry: erdify ./src/database --title "Database Schema" -o docs/erd.puml
language: system
files: ^src/database/.*\.py$
pass_filenames: false
Or using uvx (no installation required):
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: generate-erd
name: ๐๏ธ Generate ERD Diagram
entry: uvx erdify ./src/database --title "Database Schema" -o docs/erd.puml
language: system
files: ^src/database/.*\.py$
pass_filenames: false
Setup:
# Install pre-commit
pip install pre-commit
# Install the hooks
pre-commit install
# Run manually on all files
pre-commit run generate-erd --all-files
How it works:
- ๐ Only triggers when files in
src/database/change - ๐ Automatically regenerates
docs/erd.puml - โ Stages the updated diagram with your commit
- ๐ซ Fails if the diagram would change (ensuring docs stay in sync)
Tip: Add docs/erd.puml to your staged files before committing, or use the --all-files flag to regenerate.
๐ 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
- Pydantic - Data validation using Python type hints
- dataclasses - Python standard-library data classes
Rendering & output
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
- Contributor Covenant - Our Code of Conduct
- Keep a Changelog - Changelog format
- And everyone who contributes issues, ideas and pull requests ๐
Made with โค๏ธ by Devsuit GmbH
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 erdify-0.5.0rc1.tar.gz.
File metadata
- Download URL: erdify-0.5.0rc1.tar.gz
- Upload date:
- Size: 23.0 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b485237d7c4bc6f8c31494ce75eb0abfe425c40c0b5125d9753d96b24db569f6
|
|
| MD5 |
7a12e8560d83771970fff13d93f57b35
|
|
| BLAKE2b-256 |
f4e9d7f005a544d082725b5c09f3f87407132082c38fe61fe595be8c8eb83037
|
File details
Details for the file erdify-0.5.0rc1-py3-none-any.whl.
File metadata
- Download URL: erdify-0.5.0rc1-py3-none-any.whl
- Upload date:
- Size: 25.3 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e13ae06fb002e6da2b22b161e5dc1c7fd4a66dd1c2b9225a2d1871ef0a21748
|
|
| MD5 |
b401b59ff5dcf9889430c856df6517ca
|
|
| BLAKE2b-256 |
ac7bd785b5a417bac9a62dd61d666c6ab7d8af02db772f815285947f343f8ca7
|