Django ER Diagrams (With / Without AI)
I wanted an ER diagram of my django models. Django extensions is great, but could I do it with the Cody AI assistant?
Why?
In my full time job I've been using generative AI to query DDL in combination with design documents to define a starting set of domain aggregates.
The AI has been used to generate PlantUML diagrams with a very high degree of success.
One can use django-extensions graph models to generate models, but these can be quite noisy, especially if you use simple history tables. (I'm aware you can restrict models, etc. in django-extensions, but it's a phaf.)
Fig 1. An extract from a django-extensions diagram from a project.
Cody Approach
I've had great results creating diagrams with the free / premium Cody AI assistant. So I thought I'd create a pre-canned prompt for it to generate ER diagrams from models.
Prompt Rules
The full prompt is given below, but the core features I wanted are:
- Primary key in its own box
- Bullets for mandatory fields
- Crow's foot syntax on the relationships and foreign key attribute name on the line
- Dashed lines for relationships with appropriate cardinality symbols at each end.
- Annotation on entity name if it has a history table. The (H) suffix on the entity name.
- <<FK>> suffix on foreign key attributes
Results
Running the prompt against the same model as Fig 1 gave similar, but different, results from the Claude 3.5 and Chat GPT-4o LLMs.
The diagram snippets below show the same core entities as the django-extensions diagram above. It's much easier to focus on the business meaning of the data in these views where implementation detail is hidden.
Interestingly I prefer the Claud 3.5 diagrams to the GPT-4o ones.
Fig 2. Claude 3.5 model
The Claude model pretty much nailed it except that the relationship between CustomUser and SubscriptionType, while a many to many relationship, is actually realised through the UserSubscription table. Also it's made up a set of fields in the CustomUser entity (the sub-class only added the uid attribute).
Fig 3. GPT-4o model
The GPT-4o diagram has more errors; it really loves many to many relationships and it's missed the mandatory aspect of a few attributes.
However, unlike the Claude model, it has successfully worked out all the attributes of the CustomUser entity.
Conclusion
Despite the small errors from the AI generation I find the results very useful. The diagrams focus on the core entities, hiding a lot of the noise that's in the django-extensions diagram.
Cody Prompt
Here's the json used to define the Cody custom prompt.
{ "er-diagram": { "description": "Generate ER diagram from a model file.", "mode": "ask", "prompt": "Generate a plantuml information engineering diagram with crows feet syntax from the given models file. Use the following rules: 0. Use the 'as' syntax for the entity name and put the first, quoted, part in bold. 1. Flag the primary key fields with * and put --- on a new line after the primary key fields. 2. Use the * prefix for mandatory attributes, 3. Use a <<FK>> suffix for foreign key fields. 4. IMPORTANT Use dashed rather than solid lines for relationships and retain the cardinality options. 5. At the top of the file include a title with the name and path of the file. Duplicate the title to the right of the @startuml command. 6. Add skinparam linetype ortho but comment it out. 7. Add hide circle. 8. Ensure many to many relationships are identified as such use the correct notation with a crow's foot at each end. 9. Label relatioships with the attribute(s) from the child entity. 10. If the entity has a history attribute then suffix the quoted entity name with (H). 11. Add a note with the text \"AI Generated, Check for Errors\" and call it ai_note. FINALLY Review your work carefully, make sure you identify all the foreign keys and relationships, and correct any errors.", "context": { "currentFile": true } } }