How to make schema graph with sqlalchemy_schemadisplay?

540 Views Asked by At

How to make schema graph?

meta = MetaData()
meta.reflect(bind=engine, views=True, resolve_fks=False)

graph = create_schema_graph(
    metadata=meta,
    show_indexes=False,
    concentrate=False,
    rankdir='LR'
)
graph.write_png('dbschema.png')

imgplot = plt.imshow(mpimg.imread('dbschema.png'))
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.show()

I get error in create_schema_graph:

AttributeError: 'MetaData' object has no attribute 'bind'

Thats shame, but I already did it, but I can't figure out, that I doing wrong this time.

enter image description here

1

There are 1 best solutions below

0
Canna On

I have this quick & dirty code to create a schema graph, i don't know if it can help you:

from sqlalchemy import create_engine, MetaData
from sqlalchemy_schemadisplay import create_schema_graph

# Create an engine and reflect the database
engine = create_engine('postgresql://my_user:mypassword@localhost:5432/my_db')
metadata = MetaData(schema='my_schema')
metadata.reflect(bind=engine)

# Now create the schema graph
graph = create_schema_graph(
    metadata=metadata,
    show_datatypes=True,
    show_indexes=True,
    rankdir='LR',
    concentrate=False,
    show_schema_name=True
)
graph.write_png('dbschema.png')

Regards,

C