Custom Search

Thursday, July 3, 2014

How to generate ER diagram of any database using python sqlalchemy_schemadisplay

1)
Install all Dependencies


#sudo pip install configparser
#sudo pip install sqlalchemy
#sudo pip install pydot
#sudo pip install sqlalchemy_schemadisplay
#sudo pip install graphviz

#sudo apt-get install GraphViz




2)
Python Script to generate ER diagram

#!/usr/bin/env python
import ConfigParser
from sqlalchemy import MetaData
from sqlalchemy_schemadisplay import create_schema_graph

config = ConfigParser.RawConfigParser()
##Open nova.conf file
config.read('/etc/nova/nova.conf')
##Read the flag "sql_connection" from nova.conf and make connection to database
connection = config.get("DEFAULT", "sql_connection")


##Or you can directly specify the connection like

##connection = "mysql://root:password@127.0.0.1/nova?charset=utf8"

##Generate graph of connected database
graph = create_schema_graph(metadata=MetaData(connection),
                 show_datatypes=False,
                 show_indexes=False,
                 rankdir='LR',
                 concentrate=False)

##Generate png image                
graph.write_png('dbschema.png')


3)
Ref:
https://coderwall.com/p/pngadg

3 comments:

  1. #!/usr/bin/env python
    import ConfigParser
    from sqlalchemy import MetaData
    from sqlalchemy_schemadisplay import create_schema_graph

    #config = ConfigParser.RawConfigParser()
    ##Open nova.conf file
    #config.read('/etc/nova/nova.conf')
    ##Read the flag "sql_connection" from nova.conf and make connection to database
    #connection = config.get("DEFAULT", "sql_connection")


    ##Or you can directly specify the connection like
    connection = "mysql://root:jio@127.0.0.1/nova?charset=utf8"

    ##Generate graph of connected database
    graph = create_schema_graph(metadata=MetaData(connection),
    show_datatypes=False,
    show_indexes=False,
    rankdir='LR',
    concentrate=False)

    ##Generate png image
    graph.write_png('dbschema.png')

    ReplyDelete
  2. Can you please share the "dataset" for ERD generation?

    ReplyDelete
  3. can you help me to generate erd diagram from statement using python?

    ReplyDelete