Skip to main content

sqlalchemy_schemadisplay

Turn SQLAlchemy DB Model into a graph.

PyPI

See SQLAlchemy wiki for the previous version of this doc.

Usage

You will need atleast SQLAlchemy and pydot along with graphviz for this. Graphviz-cairo is highly recommended to get tolerable image quality. If PIL and an image viewer are available then there are functions to automatically show the image. Some of the stuff, specifically loading list of tables from a database via a mapper and reflecting indexes currently only work on postgres.

This is an example of database entity diagram generation:

from sqlalchemy import MetaData
from sqlalchemy_schemadisplay import create_schema_graph

# create the pydot graph object by autoloading all tables via a bound metadata object
graph = create_schema_graph(metadata=MetaData('postgres://user:pwd@host/database'),
   show_datatypes=False, # The image would get nasty big if we'd show the datatypes
   show_indexes=False, # ditto for indexes
   rankdir='LR', # From left to right (instead of top to bottom)
   concentrate=False # Don't try to join the relation lines together
)
graph.write_png('dbschema.png') # write out the file

And an UML class diagram from a model:

from myapp import model
from sqlalchemy_schemadisplay import create_uml_graph
from sqlalchemy.orm import class_mapper

# lets find all the mappers in our model
mappers = [model.__mapper__]
for attr in dir(model):
    if attr[0] == '_': continue
    try:
        cls = getattr(model, attr)
        mappers.append(cls.property.entity)
    except:
        pass

# pass them to the function and set some formatting options
graph = create_uml_graph(mappers,
    show_operations=False, # not necessary in this case
    show_multiplicity_one=False # some people like to see the ones, some don't
)
graph.write_png('schema.png') # write out the file

Changelog

2.0 - 2024-02-15

  • Compatibility with SQLAlchemy 2.x [Jonathan Chico]

  • Python requirements changed to >= 3.8 [zlopez]

1.4 - 2024-02-15

Last release to support Python 2.

  • Limit SQLAlchemy dependency to < 2.0 to fix installation for Python 2 [abitrolly - Anatoli Babenia]

  • Set dir kwarg in Edge instantiation to ‘both’ in order to show arrow heads and arrow tails. [bkrn - Aaron Di Silvestro]

  • Add ‘show_column_keys’ kwarg to ‘create_schema_graph’ to allow a PK/FK suffix to be added to columns that are primary keys/foreign keys respectively [cchrysostomou - Constantine Chrysostomou]

  • Add ‘restrict_tables’ kwarg to ‘create_schema_graph’ to restrict the desired tables we want to generate via graphviz and show relationships for [cchrysostomou - Constantine Chrysostomou]

1.3 - 2016-01-27

  • Fix warning about illegal attribute in uml generation by using correct attribute. [electrocucaracha - Victor Morales]

  • Use MIT license [fschulze]

1.2 - 2014-03-02

  • Compatibility with SQLAlchemy 0.9. [fschulze]

  • Compatibility with SQLAlchemy 0.8. [Surgo - Kosei Kitahara]

  • Leave tables out even when a foreign key points to them but they are not in the table list. [tiagosab - Tiago Saboga]

1.1 - 2011-10-12

  • New option to skip inherited attributes. [nouri]

  • Quote class name, because some names like ‘Node’ confuse dot. [nouri - Daniel Nouri]

1.0 - 2011-01-07

  • Initial release [fschulze - Florian Schulze]

  • Original releases as recipe on SQLAlchemy Wiki by Ants Aasma

Release files for sqlalchemy-schemadisplay 2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlalchemy-schemadisplay 2.0
File Size Uploaded
sqlalchemy_schemadisplay-2.0.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlalchemy-schemadisplay 2.0
File Interpreter ABI Platform
sqlalchemy_schemadisplay-2.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.0 kB

Release files / sqlalchemy_schemadisplay-2.0.tar.gz

Download URL sqlalchemy_schemadisplay-2.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e90b9c9868814975d674a889aadb7c4651658f0e119e1c9320279ea527744d5e
BLAKE2b-256 checksum
How to use checksums
4fb0d4587a6223dd563072ed5d0b94e0d062bb3d019bf16d0e65a85324c49efc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.6

Release files / sqlalchemy_schemadisplay-2.0-py3-none-any.whl

Download URL sqlalchemy_schemadisplay-2.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e4b928e2aec145f72a2b35de7855a78fca5e09ac4d48f2d58b4472cb640cd362
BLAKE2b-256 checksum
How to use checksums
1f9ea8d5cea8fb842393846ad42596eeb989511fab0aa1c88fe226c24c6355e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.6

Release history Release notifications | RSS feed

This release

2.0 This release

2 release files

1.4

2 release files

1.3

2 release files

1.3dev

1.2

1 release file

1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page