Registry / database / sqlalchemy-schemadisplay

sqlalchemy-schemadisplay

JSON →
library2.0pypypiunverified

SQLAlchemy SchemaDisplay is a Python library (version 2.0) for generating visual diagrams from SQLAlchemy ORM models or directly from a database schema. It leverages the Graphviz engine to produce high-quality visualizations of database structures, showing tables, columns, relationships, and data types. Releases are primarily driven by feature additions and compatibility updates with SQLAlchemy.

pip install sqlalchemy-schemadisplay graphviz
INSTALL
IMPORT
SIG · SQLALCHEMY-SCHEMAD
S
sqlalchemy-schemadisplay
databasepythonv2.0
Install
4.3s avg
Import
961ms
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 1.004s · 63.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.3s · import 0.917s · 63MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

create_schema_graph
from sqlalchemy_schemadisplay import create_schema_graph

This quickstart demonstrates how to define simple SQLAlchemy models using `declarative_base` and then use `create_schema_graph` to generate a PNG diagram representing their structure. It outputs a file named `schema.png` in the current directory. Remember that the Graphviz executable must be installed on your system for this to work.

import os from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.orm import declarative_base from sqlalchemy_schemadisplay import create_schema_graph # 1. Define your SQLAlchemy models Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False) email = Column(String(100), unique=True) class Product(Base): __tablename__ = 'products' id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False) price = Column(Integer) # 2. Create the graph and save it to a file try: # Using Base.metadata directly graph = create_schema_graph( metadata=Base.metadata, show_datatypes=True, show_labels=True, rankdir='LR', # Left-to-right layout orientation='portrait' ) output_file = 'schema.png' graph.write_png(output_file) print(f"Schema diagram saved to {output_file}") except Exception as e: print(f"An error occurred: {e}") print("Make sure Graphviz is installed on your system PATH and 'graphviz' Python package is installed.") # Optional: Clean up (if you created a temporary database for reflection) # os.remove('test.db') if os.path.exists('test.db') else None
schemadisplay --version
Debug
Known issues
breakingThe `create_schema_graph` function requires the external Graphviz executable (`dot` command) to be installed on your system and available in your PATH. Without it, the diagram generation will fail with a `FileNotFoundError`.
fix
Install Graphviz on your operating system (e.g., `sudo apt-get install graphviz` on Debian/Ubuntu, `brew install graphviz` on macOS, or from graphviz.org for Windows).
affects: All versions
gotchaForgetting to install the Python `graphviz` package alongside `sqlalchemy-schemadisplay` will result in a `ModuleNotFoundError` when `create_schema_graph` attempts to import it.
fix
Ensure both `sqlalchemy-schemadisplay` and `graphviz` are installed: `pip install sqlalchemy-schemadisplay graphviz`.
affects: All versions
gotchaWhen using `declarative_base`, you must pass `Base.metadata` to `create_schema_graph`, not the `Base` object itself. Passing `Base` directly will likely result in an `AttributeError`.
fix
Always use `metadata=Base.metadata` for declarative models or `metadata=db_engine.metadata` for reflected schemas.
affects: All versions
gotchaOutputting the diagram requires specifying the `filename` for methods like `write_png`, `write_svg`, etc. Also, ensure the directory has write permissions. If no filename is given, the graph object is returned but not saved.
fix
Call a `write_` method on the returned graph object with a valid file path, e.g., `graph.write_png('my_schema.png')`.
affects: All versions
Upgrade
Version history
2.0latest on PyPI · released Feb 15, 2024
Audit
Dependencies
SQLAlchemyrequiredCore ORM and schema reflection functionality.
graphvizrequiredPython binding for the Graphviz diagramming software.
Agent activity
17 hits · last 30 days
node
12
OpenAI (training)
2
Meta
1
Resources
sqlalchemy-schemadisplay — pip install sqlalchemy-schemadisplay · libregistry