Getting field metadata for use in autogenerated model diagrams (erdantic)
See original GitHub issueFirst 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:
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:
- Created 2 years ago
- Reactions:3
- Comments:6
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 😃
Here is the
__dict__
of a model instance. Of particular interest in the context of this issue might be__sqlmodel_relationships__
.