简体   繁体   English

如何自动生成ER图来可视化SQLAlchemy中ORM的表关系?

[英]How can I automatically generate an ER diagram to visualize table relationships of an ORM in SQLAlchemy?

I am trying to find a way to generate an entity relationship diagram based on an ORM which has been created using SQLAlchemy. The ORM might look like this and contain multiple tables, that are related via Primary and Foreign Keys:我正在尝试找到一种方法来生成基于使用 SQLAlchemy 创建的 ORM 的实体关系图。ORM 可能看起来像这样并包含多个表,这些表通过主键和外键关联:

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import declarative_base

# declarative base class
Base = declarative_base()

# an example mapping using the base
class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    nickname = Column(String)

I already found https://pypi.org/project/ERAlchemy/ but it doesn't help since it is only displaying general relations between tables.我已经找到https://pypi.org/project/ERAlchemy/但它没有帮助,因为它只显示表之间的一般关系。 I would like to see exactly which columns are related to each other via primary key and foreign keys though, so the arrows should be displayed exactly between the related columns.不过,我想确切地看到哪些列通过主键和外键相互关联,因此箭头应该准确地显示在相关列之间。

Anyone knows of a good alternative here?有人知道这里有一个好的选择吗?

I found my own solution in the meantime.与此同时,我找到了自己的解决方案。 It is adding a little bit more complexity, but delivers a great output. Just sharing here as well in case so else has a similar use case:它增加了一点复杂性,但提供了一个很好的 output。在这里也分享一下,以防其他人有类似的用例:

  1. I locally setup a mysql database (using the mysql docker image)我在本地设置了一个 mysql 数据库(使用 mysql docker 图像)
  2. Used Base.metadata.create_all(engine) to actually generate the (empty) tables in the local database使用 Base.metadata.create_all(engine) 在本地数据库中实际生成(空)表
  3. Used mysql workbench's database reverse engineering feature, which automatically generates an interactive ER diagram使用mysql workbench的数据库逆向工程功能,自动生成交互式ER图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM