question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting field metadata for use in autogenerated model diagrams (erdantic)

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google “How to X in SQLModel” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

See description

Description

I am trying to add SQLModel support to Erdantic, an entity relationship diagram generator. It supports Pydantic and produces diagrams that look like this:

diagram

Wanted Solution

This is hopefully more of a question than a feature request. I would like to know if it is possible to get the metadata that Erdantic needs. You can see instructions and code code showing how this is implemented for Pydantic. It is relatively short. I thought that since SQLModels are Pydantic models I might be able to reuse most of the code, but SQLModel relationship fields appear to be special, and those are crucial.

Wanted Code

The idea is as follows although the way it happens doesn’t matter as long as there’s a way to do it.

class Hero(SQLModel):
  ...

[(field.name, field.is_nullable, field.is_many, field.type) for field in Hero.fields]

As I understand it, is_many means it is on the many side of a many-to-one or many-to-many relationship

Alternatives

I have tried to figure out if there is a way to cast a SQLModel as a Pydantic model, but python doesn’t really support the idea of casting.

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

Python 3.9.5

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

1reaction
hugolyticscommented, Aug 23, 2022

Hi @DanLipsitt , did you make any progress with this? I was trying to do the same thing, I looked into generating the ERD based on the underlying sqlalchemy models, but I didn’t get that far. I would also be amazing to render the diagrams inside of mkdocs, on the other hand that would also be possible by using erdantic inside of jupyter and rendering the ipynb with mdocs-jupyter (or something like nbdev). Anyway, id love to see whether documenting datamodels can be made a little easier 😃

1reaction
StefanBrandcommented, Mar 15, 2022

Here is the __dict__ of a model instance. Of particular interest in the context of this issue might be __sqlmodel_relationships__.

mappingproxy({'Config': <class 'geo_info_svg.models.Country.Config'>,
              '__abstractmethods__': frozenset(),
              '__annotations__': {'geometry': <class 'geoalchemy2.types.Geometry'>,
                                  'id': typing.Optional[int],
                                  'name': <class 'str'>,
                                  'population': <class 'int'>},
              '__class_vars__': {'__name__',
                                 '__sqlmodel_relationships__',
                                 '__tablename__',
                                 'metadata'},
              '__config__': <class 'sqlmodel.main.Config'>,
              '__custom_root_type__': False,
              '__doc__': None,
              '__exclude_fields__': None,
              '__fields__': {'geometry': ModelField(name='geometry', type=Geometry, required=True),
                             'id': ModelField(name='id', type=Optional[int], required=False, default=None),
                             'name': ModelField(name='name', type=str, required=True),
                             'population': ModelField(name='population', type=int, required=True)},
              '__hash__': None,
              '__include_fields__': None,
              '__init__': <function __init__ at 0x7f236f753370>,
              '__json_encoder__': <staticmethod(<cyfunction pydantic_encoder at 0x7f237d1bae90>)>,
              '__mapper__': <Mapper at 0x7f236f74a320; Country>,
              '__module__': 'geo_info_svg.models',
              '__post_root_validators__': [],
              '__pre_root_validators__': [],
              '__private_attributes__': {},
              '__schema_cache__': {},
              '__signature__': <pydantic.utils.ClassAttribute object at 0x7f236f749e70>,
              '__slots__': set(),
              '__sqlmodel_relationships__': {},
              '__table__': Table('country', MetaData(), Column('geometry', Geometry(geometry_type='POLYGON', srid=3035, from_text='ST_GeomFromEWKT', name='geometry'), table=<country>), Column('id', Integer(), table=<country>, primary_key=True), Column('name', AutoString(), table=<country>, nullable=False), Column('population', Integer(), table=<country>, nullable=False), schema=None),
              '__validators__': {},
              '__weakref__': None,
              '_abc_impl': <_abc._abc_data object at 0x7f236f759800>,
              '_sa_class_manager': <ClassManager of <class 'geo_info_svg.models.Country'> at 7f236f760950>,
              'geometry': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f236f78a700>,
              'id': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f236f78a9d0>,
              'name': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f236f78aa70>,
              'population': <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f236f78ab10>})
Read more comments on GitHub >

github_iconTop Results From Across the Web

erdantic: Entity Relationship Diagrams
erdantic is a simple tool for drawing entity relationship diagrams (ERDs) for Python data model classes. Diagrams are rendered using the venerable Graphviz ......
Read more >
Use metadata to generate entity diagrams - Microsoft Learn
Learn about Metadata Diagram entity to create a diagram that shows a relationship for just one entity, or a complex diagram that includes ......
Read more >
Entity-Relationship Diagram (ERD) - Select Star
Today, we are releasing Auto-generated ERD (Entity-Relationship Diagram) in order to better support data analysts and citizen data ...
Read more >
How to create ER diagram for existing SQL Server database ...
How to visualize SQL Server database schema using database diagrams with free MS tool - SQL Server Management Studio (SSMS) 16.
Read more >
Creating an ERD for your dbt project - Show and Tell
While the auto-generated documentation for your dbt project ships with a ... to see an Entity Relation Diagram (ERD) of your project instead....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found