erdalchemy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- erdalchemy-0.1.0/.claude/settings.local.json +26 -0
- erdalchemy-0.1.0/.gitignore +20 -0
- erdalchemy-0.1.0/LICENSE +21 -0
- erdalchemy-0.1.0/PKG-INFO +153 -0
- erdalchemy-0.1.0/README.md +126 -0
- erdalchemy-0.1.0/examples/blog/blog.png +0 -0
- erdalchemy-0.1.0/examples/blog/demo.py +48 -0
- erdalchemy-0.1.0/examples/ecommerce/ecommerce.png +0 -0
- erdalchemy-0.1.0/examples/ecommerce/ecommerce.py +73 -0
- erdalchemy-0.1.0/examples/hr/hr.png +0 -0
- erdalchemy-0.1.0/examples/hr/hr.py +77 -0
- erdalchemy-0.1.0/examples/themes/blue.png +0 -0
- erdalchemy-0.1.0/examples/themes/custom.png +0 -0
- erdalchemy-0.1.0/examples/themes/dark.png +0 -0
- erdalchemy-0.1.0/examples/themes/default.png +0 -0
- erdalchemy-0.1.0/examples/themes/green.png +0 -0
- erdalchemy-0.1.0/examples/themes/rose.png +0 -0
- erdalchemy-0.1.0/examples/university/university.png +0 -0
- erdalchemy-0.1.0/examples/university/university.py +77 -0
- erdalchemy-0.1.0/pyproject.toml +36 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/__init__.py +49 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/cli.py +123 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/export.py +63 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/html_renderer.py +424 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/introspect.py +156 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/layout.py +105 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/renderer.py +216 -0
- erdalchemy-0.1.0/src/sqlalchemy_erd/theme.py +110 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"Bash(grep -E \"\\\\.py$\")",
|
|
5
|
+
"Bash(pip install *)",
|
|
6
|
+
"Bash(python examples/demo.py)",
|
|
7
|
+
"Bash(xdg-open *)",
|
|
8
|
+
"Bash(sqlalchemy-erd examples.demo:Base *)",
|
|
9
|
+
"Bash(python -c \"from PIL import Image; img = Image.open\\('erd.png'\\); print\\(f'Size: {img.size}, Mode: {img.mode}'\\)\")",
|
|
10
|
+
"Bash(git remote *)",
|
|
11
|
+
"Bash(git add *)",
|
|
12
|
+
"Bash(git commit *)",
|
|
13
|
+
"Bash(git push *)",
|
|
14
|
+
"Bash(python examples/ecommerce.py)",
|
|
15
|
+
"Bash(python examples/university.py)",
|
|
16
|
+
"Bash(python examples/hr.py)",
|
|
17
|
+
"Bash(git mv *)",
|
|
18
|
+
"Bash(python -m build)",
|
|
19
|
+
"Bash(sed -i 's|assets/demo.png|examples/blog/blog.png|g' examples/blog/demo.py)",
|
|
20
|
+
"Bash(sed -i 's|assets/ecommerce.png|examples/ecommerce/ecommerce.png|g' examples/ecommerce/ecommerce.py)",
|
|
21
|
+
"Bash(sed -i 's|assets/university.png|examples/university/university.png|g' examples/university/university.py)",
|
|
22
|
+
"Bash(sed -i 's|assets/hr.png|examples/hr/hr.png|g' examples/hr/hr.py)",
|
|
23
|
+
"Bash(python *)"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*$py.class
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.eggs/
|
|
8
|
+
*.egg
|
|
9
|
+
.venv/
|
|
10
|
+
venv/
|
|
11
|
+
.env
|
|
12
|
+
*.so
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
.pytest_cache/
|
|
15
|
+
*.html
|
|
16
|
+
!examples/**/*.html
|
|
17
|
+
*.png
|
|
18
|
+
!examples/**/*.png
|
|
19
|
+
*.pdf
|
|
20
|
+
!examples/**/*.pdf
|
erdalchemy-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jose Velazco
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: erdalchemy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Interactive ERD visualization for SQLAlchemy 2.0 models
|
|
5
|
+
Author: Jose Velazco
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: database,diagram,erd,sqlalchemy,visualization
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Database
|
|
17
|
+
Classifier: Topic :: Software Development :: Documentation
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
20
|
+
Provides-Extra: all
|
|
21
|
+
Requires-Dist: cairosvg>=2.7; extra == 'all'
|
|
22
|
+
Provides-Extra: pdf
|
|
23
|
+
Requires-Dist: cairosvg>=2.7; extra == 'pdf'
|
|
24
|
+
Provides-Extra: png
|
|
25
|
+
Requires-Dist: cairosvg>=2.7; extra == 'png'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# sqlalchemy-erd
|
|
29
|
+
|
|
30
|
+
Interactive ERD visualizer for SQLAlchemy 2.0 models. Introspects your `DeclarativeBase` metadata and generates diagram files with no manual configuration required.
|
|
31
|
+
|
|
32
|
+
- Drag-and-drop interactive HTML output
|
|
33
|
+
- Auto-layout via force-directed algorithm
|
|
34
|
+
- Hover highlighting for tables and relationships
|
|
35
|
+
- Export to HTML, SVG, PNG, and PDF
|
|
36
|
+
- Built-in color themes and per-table color overrides
|
|
37
|
+
- Zero dependencies beyond SQLAlchemy
|
|
38
|
+
|
|
39
|
+

|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install sqlalchemy-erd
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
PNG and PDF export require an optional dependency:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install "sqlalchemy-erd[all]"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick start
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from sqlalchemy_erd import generate_erd
|
|
57
|
+
from myapp.models import Base
|
|
58
|
+
|
|
59
|
+
generate_erd(Base, output="erd.html")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## CLI
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Interactive HTML (default)
|
|
66
|
+
sqlalchemy-erd myapp.models:Base
|
|
67
|
+
|
|
68
|
+
# Specific format and output path
|
|
69
|
+
sqlalchemy-erd myapp.models:Base --format svg --output erd.svg
|
|
70
|
+
|
|
71
|
+
# With theme and custom title
|
|
72
|
+
sqlalchemy-erd myapp.models:Base --format png --theme blue --title "My Schema"
|
|
73
|
+
|
|
74
|
+
# Per-table color overrides as JSON
|
|
75
|
+
sqlalchemy-erd myapp.models:Base --colors '{"users": "#1d4ed8", "orders": "#059669"}'
|
|
76
|
+
|
|
77
|
+
# High-resolution PNG
|
|
78
|
+
sqlalchemy-erd myapp.models:Base --format png --scale 3
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Python API
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from sqlalchemy_erd import generate_erd
|
|
85
|
+
from myapp.models import Base
|
|
86
|
+
|
|
87
|
+
generate_erd(Base, output="erd.html", format="html")
|
|
88
|
+
generate_erd(Base, output="erd.svg", format="svg")
|
|
89
|
+
generate_erd(Base, output="erd.png", format="png", scale=2) # requires cairosvg
|
|
90
|
+
generate_erd(Base, output="erd.pdf", format="pdf") # requires cairosvg
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Themes
|
|
94
|
+
|
|
95
|
+
Five built-in themes: `default`, `blue`, `green`, `dark`, `rose`.
|
|
96
|
+
|
|
97
|
+
Preview images for each theme are available in [`examples/themes/`](examples/themes/).
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
generate_erd(Base, theme="dark")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Per-table color overrides let you assign any hex color to individual tables while keeping the rest of the theme intact:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
generate_erd(
|
|
107
|
+
Base,
|
|
108
|
+
theme="default",
|
|
109
|
+
table_colors={
|
|
110
|
+
"users": "#1e40af",
|
|
111
|
+
"orders": "#065f46",
|
|
112
|
+
"products": "#9f1239",
|
|
113
|
+
},
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Supported column types
|
|
118
|
+
|
|
119
|
+
| SQLAlchemy type | Badge |
|
|
120
|
+
|---|---|
|
|
121
|
+
| Primary key | `PK` |
|
|
122
|
+
| Foreign key | `FK` |
|
|
123
|
+
| `String` | `string` |
|
|
124
|
+
| `Text` | `text` |
|
|
125
|
+
| `Integer` / `BigInteger` / `SmallInteger` | `int` / `bigint` / `smallint` |
|
|
126
|
+
| `Float` / `Numeric` | `float` / `numeric` |
|
|
127
|
+
| `Date` | `date` |
|
|
128
|
+
| `DateTime` | `datetime` |
|
|
129
|
+
| `Boolean` | `bool` |
|
|
130
|
+
| `JSON` | `json` |
|
|
131
|
+
| `Uuid` | `uuid` |
|
|
132
|
+
|
|
133
|
+
Nullable columns display a `?` suffix (e.g. `text?`, `date?`).
|
|
134
|
+
|
|
135
|
+
## Relationship types
|
|
136
|
+
|
|
137
|
+
| Cardinality | Line style |
|
|
138
|
+
|---|---|
|
|
139
|
+
| 1:N | Solid with arrow |
|
|
140
|
+
| N:N | Dashed with arrow |
|
|
141
|
+
|
|
142
|
+
## Examples
|
|
143
|
+
|
|
144
|
+
See the [`examples/`](examples/) directory:
|
|
145
|
+
|
|
146
|
+
- [`examples/blog/`](examples/blog/) - blog schema (User, Post, Comment)
|
|
147
|
+
- [`examples/ecommerce/`](examples/ecommerce/) - 1:N chains (Category, Product, Order, Customer, OrderItem)
|
|
148
|
+
- [`examples/university/`](examples/university/) - N:N via association tables (Student, Course, Professor, Department)
|
|
149
|
+
- [`examples/hr/`](examples/hr/) - 1:1 and 1:N (Employee, EmployeeProfile, Department, Project)
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# sqlalchemy-erd
|
|
2
|
+
|
|
3
|
+
Interactive ERD visualizer for SQLAlchemy 2.0 models. Introspects your `DeclarativeBase` metadata and generates diagram files with no manual configuration required.
|
|
4
|
+
|
|
5
|
+
- Drag-and-drop interactive HTML output
|
|
6
|
+
- Auto-layout via force-directed algorithm
|
|
7
|
+
- Hover highlighting for tables and relationships
|
|
8
|
+
- Export to HTML, SVG, PNG, and PDF
|
|
9
|
+
- Built-in color themes and per-table color overrides
|
|
10
|
+
- Zero dependencies beyond SQLAlchemy
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install sqlalchemy-erd
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
PNG and PDF export require an optional dependency:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install "sqlalchemy-erd[all]"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from sqlalchemy_erd import generate_erd
|
|
30
|
+
from myapp.models import Base
|
|
31
|
+
|
|
32
|
+
generate_erd(Base, output="erd.html")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## CLI
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# Interactive HTML (default)
|
|
39
|
+
sqlalchemy-erd myapp.models:Base
|
|
40
|
+
|
|
41
|
+
# Specific format and output path
|
|
42
|
+
sqlalchemy-erd myapp.models:Base --format svg --output erd.svg
|
|
43
|
+
|
|
44
|
+
# With theme and custom title
|
|
45
|
+
sqlalchemy-erd myapp.models:Base --format png --theme blue --title "My Schema"
|
|
46
|
+
|
|
47
|
+
# Per-table color overrides as JSON
|
|
48
|
+
sqlalchemy-erd myapp.models:Base --colors '{"users": "#1d4ed8", "orders": "#059669"}'
|
|
49
|
+
|
|
50
|
+
# High-resolution PNG
|
|
51
|
+
sqlalchemy-erd myapp.models:Base --format png --scale 3
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Python API
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from sqlalchemy_erd import generate_erd
|
|
58
|
+
from myapp.models import Base
|
|
59
|
+
|
|
60
|
+
generate_erd(Base, output="erd.html", format="html")
|
|
61
|
+
generate_erd(Base, output="erd.svg", format="svg")
|
|
62
|
+
generate_erd(Base, output="erd.png", format="png", scale=2) # requires cairosvg
|
|
63
|
+
generate_erd(Base, output="erd.pdf", format="pdf") # requires cairosvg
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Themes
|
|
67
|
+
|
|
68
|
+
Five built-in themes: `default`, `blue`, `green`, `dark`, `rose`.
|
|
69
|
+
|
|
70
|
+
Preview images for each theme are available in [`examples/themes/`](examples/themes/).
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
generate_erd(Base, theme="dark")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Per-table color overrides let you assign any hex color to individual tables while keeping the rest of the theme intact:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
generate_erd(
|
|
80
|
+
Base,
|
|
81
|
+
theme="default",
|
|
82
|
+
table_colors={
|
|
83
|
+
"users": "#1e40af",
|
|
84
|
+
"orders": "#065f46",
|
|
85
|
+
"products": "#9f1239",
|
|
86
|
+
},
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Supported column types
|
|
91
|
+
|
|
92
|
+
| SQLAlchemy type | Badge |
|
|
93
|
+
|---|---|
|
|
94
|
+
| Primary key | `PK` |
|
|
95
|
+
| Foreign key | `FK` |
|
|
96
|
+
| `String` | `string` |
|
|
97
|
+
| `Text` | `text` |
|
|
98
|
+
| `Integer` / `BigInteger` / `SmallInteger` | `int` / `bigint` / `smallint` |
|
|
99
|
+
| `Float` / `Numeric` | `float` / `numeric` |
|
|
100
|
+
| `Date` | `date` |
|
|
101
|
+
| `DateTime` | `datetime` |
|
|
102
|
+
| `Boolean` | `bool` |
|
|
103
|
+
| `JSON` | `json` |
|
|
104
|
+
| `Uuid` | `uuid` |
|
|
105
|
+
|
|
106
|
+
Nullable columns display a `?` suffix (e.g. `text?`, `date?`).
|
|
107
|
+
|
|
108
|
+
## Relationship types
|
|
109
|
+
|
|
110
|
+
| Cardinality | Line style |
|
|
111
|
+
|---|---|
|
|
112
|
+
| 1:N | Solid with arrow |
|
|
113
|
+
| N:N | Dashed with arrow |
|
|
114
|
+
|
|
115
|
+
## Examples
|
|
116
|
+
|
|
117
|
+
See the [`examples/`](examples/) directory:
|
|
118
|
+
|
|
119
|
+
- [`examples/blog/`](examples/blog/) - blog schema (User, Post, Comment)
|
|
120
|
+
- [`examples/ecommerce/`](examples/ecommerce/) - 1:N chains (Category, Product, Order, Customer, OrderItem)
|
|
121
|
+
- [`examples/university/`](examples/university/) - N:N via association tables (Student, Course, Professor, Department)
|
|
122
|
+
- [`examples/hr/`](examples/hr/) - 1:1 and 1:N (Employee, EmployeeProfile, Department, Project)
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT
|
|
Binary file
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Blog schema — basic demo showing 1:N relationships."""
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import ForeignKey, String, Text, DateTime
|
|
4
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Base(DeclarativeBase):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class User(Base):
|
|
13
|
+
__tablename__ = "users"
|
|
14
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
15
|
+
username: Mapped[str] = mapped_column(String(100))
|
|
16
|
+
email: Mapped[str] = mapped_column(String(200))
|
|
17
|
+
bio: Mapped[str | None] = mapped_column(Text)
|
|
18
|
+
posts: Mapped[list["Post"]] = relationship(back_populates="author")
|
|
19
|
+
comments: Mapped[list["Comment"]] = relationship(back_populates="author")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Post(Base):
|
|
23
|
+
__tablename__ = "posts"
|
|
24
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
25
|
+
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
|
26
|
+
title: Mapped[str] = mapped_column(String(300))
|
|
27
|
+
body: Mapped[str] = mapped_column(Text)
|
|
28
|
+
published_at: Mapped[datetime | None] = mapped_column(DateTime)
|
|
29
|
+
author: Mapped["User"] = relationship(back_populates="posts")
|
|
30
|
+
comments: Mapped[list["Comment"]] = relationship(back_populates="post")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Comment(Base):
|
|
34
|
+
__tablename__ = "comments"
|
|
35
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
36
|
+
post_id: Mapped[int] = mapped_column(ForeignKey("posts.id"))
|
|
37
|
+
author_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
|
38
|
+
body: Mapped[str] = mapped_column(Text)
|
|
39
|
+
created_at: Mapped[datetime] = mapped_column(DateTime)
|
|
40
|
+
post: Mapped["Post"] = relationship(back_populates="comments")
|
|
41
|
+
author: Mapped["User"] = relationship(back_populates="comments")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
from sqlalchemy_erd import generate_erd
|
|
46
|
+
|
|
47
|
+
generate_erd(Base, output="examples/blog/blog.png", format="png", title="Blog", scale=2)
|
|
48
|
+
print("Generated examples/blog/blog.png")
|
|
Binary file
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""E-commerce schema — 1:N relationship chains."""
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import ForeignKey, String, Text, Numeric, Integer, DateTime
|
|
4
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Base(DeclarativeBase):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Category(Base):
|
|
14
|
+
__tablename__ = "categories"
|
|
15
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
16
|
+
name: Mapped[str] = mapped_column(String(100))
|
|
17
|
+
description: Mapped[str | None] = mapped_column(Text)
|
|
18
|
+
products: Mapped[list["Product"]] = relationship(back_populates="category")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Product(Base):
|
|
22
|
+
__tablename__ = "products"
|
|
23
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
24
|
+
category_id: Mapped[int] = mapped_column(ForeignKey("categories.id"))
|
|
25
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
26
|
+
price: Mapped[Decimal] = mapped_column(Numeric(10, 2))
|
|
27
|
+
stock: Mapped[int] = mapped_column(Integer)
|
|
28
|
+
category: Mapped["Category"] = relationship(back_populates="products")
|
|
29
|
+
order_items: Mapped[list["OrderItem"]] = relationship(back_populates="product")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Customer(Base):
|
|
33
|
+
__tablename__ = "customers"
|
|
34
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
35
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
36
|
+
email: Mapped[str] = mapped_column(String(200))
|
|
37
|
+
phone: Mapped[str | None] = mapped_column(String(20))
|
|
38
|
+
orders: Mapped[list["Order"]] = relationship(back_populates="customer")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Order(Base):
|
|
42
|
+
__tablename__ = "orders"
|
|
43
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
44
|
+
customer_id: Mapped[int] = mapped_column(ForeignKey("customers.id"))
|
|
45
|
+
created_at: Mapped[datetime] = mapped_column(DateTime)
|
|
46
|
+
status: Mapped[str] = mapped_column(String(50))
|
|
47
|
+
customer: Mapped["Customer"] = relationship(back_populates="orders")
|
|
48
|
+
items: Mapped[list["OrderItem"]] = relationship(back_populates="order")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class OrderItem(Base):
|
|
52
|
+
__tablename__ = "order_items"
|
|
53
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
54
|
+
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"))
|
|
55
|
+
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"))
|
|
56
|
+
quantity: Mapped[int] = mapped_column(Integer)
|
|
57
|
+
unit_price: Mapped[Decimal] = mapped_column(Numeric(10, 2))
|
|
58
|
+
order: Mapped["Order"] = relationship(back_populates="items")
|
|
59
|
+
product: Mapped["Product"] = relationship(back_populates="order_items")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
if __name__ == "__main__":
|
|
63
|
+
from sqlalchemy_erd import generate_erd
|
|
64
|
+
|
|
65
|
+
generate_erd(
|
|
66
|
+
Base,
|
|
67
|
+
output="examples/ecommerce/ecommerce.png",
|
|
68
|
+
format="png",
|
|
69
|
+
theme="default",
|
|
70
|
+
title="E-commerce (1:N)",
|
|
71
|
+
scale=2,
|
|
72
|
+
)
|
|
73
|
+
print("Generated examples/ecommerce/ecommerce.png")
|
|
Binary file
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""HR schema — 1:1 relationships alongside 1:N."""
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import ForeignKey, String, Text, Date, Numeric
|
|
4
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
5
|
+
from datetime import date
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Base(DeclarativeBase):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Department(Base):
|
|
14
|
+
__tablename__ = "departments"
|
|
15
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
16
|
+
name: Mapped[str] = mapped_column(String(100))
|
|
17
|
+
budget: Mapped[Decimal | None] = mapped_column(Numeric(15, 2))
|
|
18
|
+
employees: Mapped[list["Employee"]] = relationship(
|
|
19
|
+
back_populates="department", foreign_keys="Employee.department_id"
|
|
20
|
+
)
|
|
21
|
+
manager: Mapped["Employee | None"] = relationship(
|
|
22
|
+
back_populates="managed_department", foreign_keys="Employee.manages_department_id"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Employee(Base):
|
|
27
|
+
__tablename__ = "employees"
|
|
28
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
29
|
+
department_id: Mapped[int] = mapped_column(ForeignKey("departments.id"))
|
|
30
|
+
manages_department_id: Mapped[int | None] = mapped_column(ForeignKey("departments.id"))
|
|
31
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
32
|
+
position: Mapped[str] = mapped_column(String(100))
|
|
33
|
+
salary: Mapped[Decimal] = mapped_column(Numeric(12, 2))
|
|
34
|
+
hire_date: Mapped[date] = mapped_column(Date)
|
|
35
|
+
department: Mapped["Department"] = relationship(
|
|
36
|
+
back_populates="employees", foreign_keys=[department_id]
|
|
37
|
+
)
|
|
38
|
+
managed_department: Mapped["Department | None"] = relationship(
|
|
39
|
+
back_populates="manager", foreign_keys=[manages_department_id]
|
|
40
|
+
)
|
|
41
|
+
profile: Mapped["EmployeeProfile | None"] = relationship(back_populates="employee")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class EmployeeProfile(Base):
|
|
45
|
+
__tablename__ = "employee_profiles"
|
|
46
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
47
|
+
employee_id: Mapped[int] = mapped_column(ForeignKey("employees.id"), unique=True)
|
|
48
|
+
bio: Mapped[str | None] = mapped_column(Text)
|
|
49
|
+
phone: Mapped[str | None] = mapped_column(String(20))
|
|
50
|
+
address: Mapped[str | None] = mapped_column(Text)
|
|
51
|
+
emergency_contact: Mapped[str | None] = mapped_column(String(200))
|
|
52
|
+
employee: Mapped["Employee"] = relationship(back_populates="profile")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Project(Base):
|
|
56
|
+
__tablename__ = "projects"
|
|
57
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
58
|
+
department_id: Mapped[int] = mapped_column(ForeignKey("departments.id"))
|
|
59
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
60
|
+
start_date: Mapped[date] = mapped_column(Date)
|
|
61
|
+
end_date: Mapped[date | None] = mapped_column(Date)
|
|
62
|
+
budget: Mapped[Decimal | None] = mapped_column(Numeric(15, 2))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
from sqlalchemy_erd import generate_erd
|
|
67
|
+
|
|
68
|
+
generate_erd(
|
|
69
|
+
Base,
|
|
70
|
+
output="examples/hr/hr.png",
|
|
71
|
+
format="png",
|
|
72
|
+
theme="green",
|
|
73
|
+
title="HR (1:1, 1:N)",
|
|
74
|
+
scale=2,
|
|
75
|
+
table_colors={"employee_profiles": "#1e40af"},
|
|
76
|
+
)
|
|
77
|
+
print("Generated examples/hr/hr.png")
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""University schema — N:N relationships via association tables."""
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import ForeignKey, String, Text, Date, Table, Column
|
|
4
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
|
5
|
+
from datetime import date
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Base(DeclarativeBase):
|
|
9
|
+
pass
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
enrollment = Table(
|
|
13
|
+
"enrollments",
|
|
14
|
+
Base.metadata,
|
|
15
|
+
Column("student_id", ForeignKey("students.id"), primary_key=True),
|
|
16
|
+
Column("course_id", ForeignKey("courses.id"), primary_key=True),
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
teaching = Table(
|
|
20
|
+
"teaching",
|
|
21
|
+
Base.metadata,
|
|
22
|
+
Column("professor_id", ForeignKey("professors.id"), primary_key=True),
|
|
23
|
+
Column("course_id", ForeignKey("courses.id"), primary_key=True),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Department(Base):
|
|
28
|
+
__tablename__ = "departments"
|
|
29
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
30
|
+
name: Mapped[str] = mapped_column(String(100))
|
|
31
|
+
building: Mapped[str | None] = mapped_column(String(100))
|
|
32
|
+
courses: Mapped[list["Course"]] = relationship(back_populates="department")
|
|
33
|
+
professors: Mapped[list["Professor"]] = relationship(back_populates="department")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Professor(Base):
|
|
37
|
+
__tablename__ = "professors"
|
|
38
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
39
|
+
department_id: Mapped[int] = mapped_column(ForeignKey("departments.id"))
|
|
40
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
41
|
+
email: Mapped[str] = mapped_column(String(200))
|
|
42
|
+
department: Mapped["Department"] = relationship(back_populates="professors")
|
|
43
|
+
courses: Mapped[list["Course"]] = relationship(secondary=teaching, back_populates="professors")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Course(Base):
|
|
47
|
+
__tablename__ = "courses"
|
|
48
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
49
|
+
department_id: Mapped[int] = mapped_column(ForeignKey("departments.id"))
|
|
50
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
51
|
+
description: Mapped[str | None] = mapped_column(Text)
|
|
52
|
+
department: Mapped["Department"] = relationship(back_populates="courses")
|
|
53
|
+
students: Mapped[list["Student"]] = relationship(secondary=enrollment, back_populates="courses")
|
|
54
|
+
professors: Mapped[list["Professor"]] = relationship(secondary=teaching, back_populates="courses")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Student(Base):
|
|
58
|
+
__tablename__ = "students"
|
|
59
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
60
|
+
name: Mapped[str] = mapped_column(String(200))
|
|
61
|
+
email: Mapped[str] = mapped_column(String(200))
|
|
62
|
+
enrollment_date: Mapped[date] = mapped_column(Date)
|
|
63
|
+
courses: Mapped[list["Course"]] = relationship(secondary=enrollment, back_populates="students")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
from sqlalchemy_erd import generate_erd
|
|
68
|
+
|
|
69
|
+
generate_erd(
|
|
70
|
+
Base,
|
|
71
|
+
output="examples/university/university.png",
|
|
72
|
+
format="png",
|
|
73
|
+
theme="blue",
|
|
74
|
+
title="University (N:N)",
|
|
75
|
+
scale=2,
|
|
76
|
+
)
|
|
77
|
+
print("Generated examples/university/university.png")
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "erdalchemy"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Interactive ERD visualization for SQLAlchemy 2.0 models"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "Jose Velazco" }]
|
|
13
|
+
keywords = ["sqlalchemy", "erd", "diagram", "visualization", "database"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Database",
|
|
23
|
+
"Topic :: Software Development :: Documentation",
|
|
24
|
+
]
|
|
25
|
+
dependencies = ["sqlalchemy>=2.0"]
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
png = ["cairosvg>=2.7"]
|
|
29
|
+
pdf = ["cairosvg>=2.7"]
|
|
30
|
+
all = ["cairosvg>=2.7"]
|
|
31
|
+
|
|
32
|
+
[project.scripts]
|
|
33
|
+
sqlalchemy-erd = "sqlalchemy_erd.cli:main"
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.wheel]
|
|
36
|
+
packages = ["src/sqlalchemy_erd"]
|