How josevelazcoh/erdalchemy works
1. Purpose and top-level flow
This repository is a self-contained ERD generator for SQLAlchemy 2.0 models. The main pipeline is: take a DeclarativeBase, introspect tables and foreign keys, build an internal schema graph, lay it out, route edges, and export the result as interactive HTML or static image formats.
2. Primary entry points
There are two user-facing entry points:
- the Python API exposed from sqlalchemy_erd via generate_erd(...)
- the CLI in src/sqlalchemy_erd/cli.py, which loads a target base class from a module path like myapp.models:Base
Both paths converge into the same core generation pipeline.
3. Schema introspection boundary
introspect.py is the boundary between SQLAlchemy models and the renderer. It reads mapped tables, columns, schemas, and foreign-key relationships from the provided declarative base and turns them into the repository’s internal graph representation. This is where multi-schema support and table/column filtering are applied.
4. Internal model and serialization
serialization.py appears to define the portable data shape used across the rest of the pipeline. That matters because the layout and rendering subsystems should not depend directly on SQLAlchemy objects; they consume a normalized schema graph instead.
5. Layout subsystem
The layout layer is split into selectable strategies:
- layered.py for the default hierarchical FK-driven layout
- force.py for force-directed placement
- star.py for hub-and-spoke or disconnected schemas
- layout_select.py chooses the strategy
- layout.py provides shared layout primitives/data structures
This is the main architectural decision point for how tables are positioned on the canvas.
6. Edge routing subsystem
edge_routing.py handles orthogonal routing of foreign-key edges so connectors avoid cutting through table cards. This is separate from node placement, which is important for diagram readability: layout decides where tables sit, routing decides how relationships are drawn between them.
7. Rendering and export
Rendering is split from layout:
- renderer.py / render.py are the core rendering layer
- html_renderer.py generates the interactive browser-based output
- export.py manages output formats such as HTML, SVG, PNG, and PDF
The README indicates PNG/PDF need an optional dependency, so static image export is an add-on boundary rather than a hard runtime dependency.
8. Theming and presentation
theme.py controls visual styling, including built-in themes and per-table color overrides. This is the presentation layer that sits on top of the structural diagram, allowing the same schema graph to be rendered with different visual identities.
9. Interactive HTML as a first-class output
The flagship output is the interactive HTML diagram: draggable cards, hover highlighting, and routed connectors. That makes the browser output the most feature-rich artifact, while SVG/PNG/PDF are simpler export targets derived from the same underlying diagram model.
10. Filtering and scale management
The repo supports regex-based include/exclude filtering for tables and columns, which is an important scaling mechanism for large schemas. This likely happens before layout so the graph size is reduced early and all downstream steps work on a smaller subgraph.
11. Examples as architecture references
The examples/ directory is not just demos; it demonstrates the major schema shapes the system is designed to handle:
- simple blog/ecommerce chains
- N:N association tables
- 1:1 and 1:N relationships
- multi-schema cross-schema foreign keys
- larger showcase graphs
These examples show the intended input diversity and validate the layout strategies.
12. Repo constraints and runtime boundary
The project emphasizes “zero dependencies beyond SQLAlchemy” for core use, with optional extras for richer exports. That constraint strongly shapes the architecture: introspection, layout, routing, and HTML generation are all implemented inside the package rather than delegated to heavier external graphing libraries.
Main entry points: tests Behavior tests · layout_select.py Strategy picker · renderer.py Renderer · __init__.py Public API
Components 19
Source package
Core ERD generation pipeline
- Public API · Exports generate_erd and package-level entry for Python users
src/sqlalchemy_erd/__init__.py - CLI · Command-line entry that loads a DeclarativeBase from a module path
src/sqlalchemy_erd/cli.py - Introspection · Reads SQLAlchemy metadata, tables, schemas, and foreign keys into the internal model
src/sqlalchemy_erd/introspect.py - Schema model · Normalized portable representation shared by layout and rendering
src/sqlalchemy_erd/serialization.py - Strategy picker · Chooses the layout algorithm for the current schema shape
src/sqlalchemy_erd/layout_select.py - Layout core · Shared layout data structures and helpers
src/sqlalchemy_erd/layout.py - Layered layout · Default hierarchical FK-driven positioning
src/sqlalchemy_erd/layered.py - Force layout · Force-directed placement for denser schemas
src/sqlalchemy_erd/force.py - Star layout · Hub-and-spoke layout for disconnected or centered schemas
src/sqlalchemy_erd/star.py - Edge routing · Routes foreign-key connectors around table cards
src/sqlalchemy_erd/edge_routing.py - Render core · Transforms the laid-out schema into drawable diagram elements
src/sqlalchemy_erd/render.py - Renderer · Main rendering orchestration and view composition
src/sqlalchemy_erd/renderer.py - HTML output · Generates the interactive browser-based ERD artifact
src/sqlalchemy_erd/html_renderer.py - Export · Writes HTML and optional static image outputs
src/sqlalchemy_erd/export.py - Theme · Controls visual styling and per-table color overrides
src/sqlalchemy_erd/theme.py
Examples
Usage and schema shape references
- Blog demo · Simple relationship example used to demonstrate the generator
examples/blog/demo.py - Multi-schema demo · Shows cross-schema foreign keys and schema grouping
examples/multi_schema/multi_schema.py - Showcase demo · Larger reference schema for layout and rendering behavior
examples/showcase/showcase.py
Tests
Behavior coverage for the main subsystems
- Behavior tests · Exercises introspection, layout, rendering, export, CLI, and theming
tests
Connections 24
- Public API → Introspection: generate ERD · Python API enters the schema scanning pipeline
- CLI → Public API: invoke · CLI resolves the target base class then calls the public API
- Introspection → Schema model: normalize · Converts SQLAlchemy metadata into the internal schema model
- Schema model → Strategy picker: dispatch · Hands the normalized schema to layout selection
- Strategy picker → Layered layout: default · Uses the hierarchical FK-driven strategy when appropriate
- Strategy picker → Force layout: alternative · Selects force-directed placement for dense graphs
- Strategy picker → Star layout: alternative · Selects hub-and-spoke placement for centered or sparse graphs
- Layered layout → Layout core: uses · Builds on shared layout primitives
- Force layout → Layout core: uses · Builds on shared layout primitives
- Star layout → Layout core: uses · Builds on shared layout primitives
- Layout core → Edge routing: prepare edges · Provides node geometry for connector routing
- Edge routing → Render core: routed diagram · Feeds routed connectors into renderable elements
- Render core → Renderer: compose · Renderer assembles the final diagram representation
- Renderer → Theme: style · Applies theme and table-level presentation overrides
- Renderer → HTML output: interactive output · Produces the browser-based HTML artifact
- Renderer → Export: static formats · Exports SVG, PNG, and PDF variants
- Blog demo → Public API: demonstrates · Example exercises the public generation flow
- Multi-schema demo → Public API: demonstrates · Example exercises multi-schema introspection and filtering
- Showcase demo → Public API: demonstrates · Example showcases larger schema and layout behavior
- Behavior tests → Introspection: covers · Tests schema scanning and filtering behavior
- Behavior tests → Strategy picker: covers · Tests strategy selection and layout behavior
- Behavior tests → Edge routing: covers · Tests connector routing behavior
- Behavior tests → Renderer: covers · Tests diagram rendering and presentation behavior
- Behavior tests → Export: covers · Tests export formats and output handling