Skip to main content

Turn SQLAlchemy DB Model into a graph

Project description

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

1.4 - 2024-02-15

Last release to support Python 2.

  • Limit SQLAlchemy dependency to < 2.0 to fix installation for Python 2

  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_schemadisplay-1.4.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_schemadisplay-1.4-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_schemadisplay-1.4.tar.gz.

File metadata

File hashes

Hashes for sqlalchemy_schemadisplay-1.4.tar.gz
Algorithm Hash digest
SHA256 6d9a815dc6b411d519805c056c40ba7448f3572e9abdfbf9c246f7a37e7b1afc
MD5 94b4e3996cabef4ecc16bf60b4703740
BLAKE2b-256 8660d23500abfb2d675274f4651abb91d7510c7362fcd58e5189e9cc31526c4f

See more details on using hashes here.

File details

Details for the file sqlalchemy_schemadisplay-1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_schemadisplay-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 68b24ff4b84e7c877d2c35bd199eff5e3460bbc44ccf898f5b58998281caf053
MD5 04bac6c1fbc5d222257e302fafd744fe
BLAKE2b-256 a9d76c8dbb6edf2e3ef60a82fdea759e3885036b6b7510b859180f0d69a567e1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page