SQLAlchemy ER diagram in python 3

后端 未结 3 2082
Happy的楠姐
Happy的楠姐 2021-02-05 18:11

Does anyone know a way to make an ER diagram from SQLAlchemy models in python 3. I found sqlalchemy_schemadisplay, which is python 2 because of pydot and ERAlchemy which is also

相关标签:
3条回答
  • 2021-02-05 18:55

    You can try eralchemy.

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import pandas as pd
    from eralchemy import render_er
    
    from sqlalchemy import (MetaData, Table, Column)    
    metadata = MetaData()
    
    # create your own model ....
    users = Table('users', metadata,
        Column('user_id', Integer(), primary_key=True),
        Column('username', String(15), nullable=False, unique=True),
    )    
    orders = Table('orders', metadata,
        Column('order_id', Integer()),
        Column('user_id', ForeignKey('users.user_id')),
    )
    # add your own table ....
    
    # Show ER model from here
    filename = 'mymodel.png'
    render_er(metadata, filename)
    imgplot = plt.imshow(mpimg.imread(filename))
    plt.rcParams["figure.figsize"] = (15,10)
    plt.show()
    

    Then it shows the model.

    Those modules I used are:

    Software Version
    Python 3.4.5 64bit
    IPython 5.1.0
    OS Windows 10
    sqlalchemy 1.1.5
    eralchemy 1.1.0
    matplotlib 2.0.0

    0 讨论(0)
  • 2021-02-05 19:11

    SQLAlchemy_SchemaDisplay works for me, too.

    On Windows I installed Graphviz and these requirements via pip:

    • pydot
    • sqlalchemy
    • sqlalchemy_schemadisplay
    • graphviz

    Then I added the Grapviz binary (bin) folder to the path and ran the code from the example at https://github.com/sqlalchemy/sqlalchemy/wiki/SchemaDisplay

    0 讨论(0)
  • 2021-02-05 19:14

    As mentioned in an earlier answer, sqlalchemy_schemadisplay is a fantastically simple tool. Here's the basic how you would use it:

    from sqlalchemy_schemadisplay import create_schema_graph
    from sqlalchemy import MetaData
    
    graph = create_schema_graph(metadata=MetaData('postgres://user:pwd@host/database'))
    graph.write_png('my_erd.png')
    
    0 讨论(0)
提交回复
热议问题