I made this last week for fun, but I think it could be useful for people. If your application has pretty complicated SQLalchemy data models (and the app I'm working on now has gotten super complicated, with dozens of models with complex interconnections), then this very easily generates an attractive SVG that you can open in the browser showing all your tables and how they are connected. You can be up and running with this in less than a minute— just open it in Google Colab, replace the sample data models with your own, and run the notebook, and you should see the SVG displayed almost immediately at the bottom.
I attempted to also generate an HTML file containing the embedded SVG as well as some JS to enable nice interactive hover-on effects (I wanted to highlight the border of the table you hover over and then change all the outgoing connections to other tables to be red, and all incoming connections to that table to be green), but it wasn't working and started turning into a slog after an hour. If anyone knows how to do this, please submit a PR!
Cool, I'm surprised I couldn't find this when I was looking for something like this originally. Probably because I was using different terminology-- "data model" instead of "schema," and "visualize" instead of "display".
Yeah, it was a bit weird to me that I couldn’t find anything like this either, which is why I made it. Seems like all the visualization tools out there go from the database tables directly, but that defeats the cross-database portability of SQLAlchemy which is one of its best features: it works equally well without any changes on SQLite, Postgres, etc.
In regards to difficulty finding alternatives, many database management tools (like DataGrip and Dbeaver) have ER diagram generators that work for all the types of databases the tools work for... so basically vendor agnostic. Other tools like Visio (blast from the past) do too.
Cool. Yeah it would seem to limit the audience a lot that it's for Django only and also has to be integrated into an existing Django project for it to work. I wanted a very small and simple tool that worked generically for any SQLAlchemy models. The core code of my tool is literally ~90 lines of Python-- the other ~200 lines are all the sample data models I include. That's because all the real heavy lifting is done by graphviz.
I see this is using Graphviz. The first time I used DOT, I was writing a parser and wanted to visualize my AST. It was fantastic and so easy to write, especially if you have a wrapper library. It's such a great tool for being able to visualize your graphs.
This is great, especially to introduce new devs to models. I use (and love) TablePlus (https://tableplus.com/) which has a diagram generator plugin that does the same!
I don't have a formal compsci background but one of the most enlightening things I've undertaken recently was a course involving data modeling. this seems like an important exercise many overlook.
>>> from sqlalchemy_data_model_visualizer import generate_data_model_diagram
python3.11/site-packages/sqlalchemy_data_model_visualizer.py", line 5, in <module>
from sqlalchemy.orm import sessionmaker, declarative_base, relationship
ImportError: cannot import name 'declarative_base' from 'sqlalchemy.orm'
18 comments
[ 0.24 ms ] story [ 30.6 ms ] threadI attempted to also generate an HTML file containing the embedded SVG as well as some JS to enable nice interactive hover-on effects (I wanted to highlight the border of the table you hover over and then change all the outgoing connections to other tables to be red, and all incoming connections to that table to be green), but it wasn't working and started turning into a slog after an hour. If anyone knows how to do this, please submit a PR!
Useful to have in a CICD pipeline so you always have an up-to-date schema diagram available.
In regards to difficulty finding alternatives, many database management tools (like DataGrip and Dbeaver) have ER diagram generators that work for all the types of databases the tools work for... so basically vendor agnostic. Other tools like Visio (blast from the past) do too.
python3.11/site-packages/sqlalchemy_data_model_visualizer.py", line 5, in <module> from sqlalchemy.orm import sessionmaker, declarative_base, relationship ImportError: cannot import name 'declarative_base' from 'sqlalchemy.orm'
doesn't work with the older sqlalchemy