Skip to content

Instantly share code, notes, and snippets.

@theskumar
Created November 17, 2014 06:47
Show Gist options
  • Save theskumar/1a2737bc070cd0b9e9a1 to your computer and use it in GitHub Desktop.
Save theskumar/1a2737bc070cd0b9e9a1 to your computer and use it in GitHub Desktop.
Database diagram using sqlalchemy
# -*- coding: utf-8 -*-
''' Generates database schema graph from a relational database.
Usages:
Add database configuation in this file and then
python app.py
Note: You must have your latest database schema in the database
engine you are running against.
'''
from __future__ import unicode_literals, absolute_import
from sqlalchemy import MetaData
from sqlalchemy_schemadisplay import create_schema_graph
# Change here
db_name = 'your_db_name_here'
db_type = 'postgres' # postgres, mysql
DATABASE_CONFIG = {
'db_name': db_name,
'db_type': db_type,
'db_host': '127.0.0.1',
}
database_url_template = "%(db_type)s://%(db_host)s/%(db_name)s"
# database_url = 'mysql://root:[email protected]/your_db_name'
# database_url = 'postgres://127.0.0.1/onydo'
database_url = database_url_template % DATABASE_CONFIG
graph = create_schema_graph(metadata=MetaData(database_url),
show_datatypes=False,
show_indexes=False,
rankdir='TB',
concentrate=True,)
graph.write_png('%s_dbschema.png' % db_name)
graph.write_svg('%s_dbschema.svg' % db_name)
graph.write_pdf('%s_dbschema.pdf' % db_name)
https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz\#md5\=9be0fcdcc595199c646ab317c1d9a709
pydot
sqlalchemy
sqlalchemy_schemadisplay
graphviz
# Database drivers (uncomment whichever you need)
# ---------------------------------
# mysql-python
# psycopg2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment