Repository navigation
fix(er): draw SQLAlchemy primary and foreign keys in D2, PlantUML and DOT - #356
Conversation
… DOT These formats marked only the first column of a composite primary key and drew each SQLAlchemy foreign key column as a typeless ForeignKey row. Use the rule DBML got in FROWNINGdev#210 for all four emitters. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
py-1.18.4: migration-risk --only and the staged-files pre-commit hook (#355), positional operation arguments (#359), abstract-base fields in scan and ER (#354), ER fixes for SQLAlchemy keys and same-named models (#356, #357), checkout-relative annotation paths (#358). 0.22.4: the ER view and hover cards show fields from abstract bases (#354). Co-authored-by: FROWNINGdev <[email protected]>
Summary
For a SQLAlchemy table,
er --format d2,plantumlanddotmarked only the first column of a composite primary key and drew each foreign key column as aForeignKeyrow with no type. #210 fixed this for DBML only. Take thisOrderLine, from the #210 tests, whose key(order_id, tenant_id)is also a foreign key toOrder:order_id: ForeignKey {constraint: foreign_key}; no primary key onOrderLine;Order.tenant_idnot a keyorder_id: Integer {constraint: [primary_key; foreign_key]}, bothtenant_idrows are keys* order_id : Integer, thenorder_id : ForeignKeyagain below the line* order_id : Integer <<FK>>,* tenant_id : IntegerOrderLinehas no PK row;order_idisForeignKey · FKorder_idisInteger · PK · FK, bothtenant_idrows arePKThe DBML rule from #210 is now two helpers,
_is_column()(a SQLAlchemy foreign key is a typed column; onlyrelationship()attributes and Django relation fields are drawn as relation rows) and_primary_key_columns()(everyprimary_key=Truecolumn of a SQLAlchemy table, or Django's one PK field). All four emitters use them. DBML output is unchanged, and so is the output for Django models: their columns and PK come out exactly as before.Type of change
Test plan
test_composite_constraint_preserves_column_types_and_primary_keys_in_every_formatintests/test_sqlalchemy_parser.pybuilds the fix(sqlalchemy): draw composite foreign key relations #210 fixture and checks the D2, PlantUML and DOT rows for both primary key columns and the foreign key. It fails onmainand passes with the fix.cd cli && python -m pytest -q: 1359 passed, 6 skipped.ruff checkandmypyare clean on the changed files.ForeignKey("order.id")column in all four formats and compared them by eye.Checklist
cd cli && pytest -qfor Python,npm testfor TypeScript) and it is green## [Unreleased]mcp_server.pyand the relevant tests intest_mcp_server.py(not touched)## Summaryabove and suggested a migration path (not breaking)Related issues / discussions
Refs #210
This change was written with the help of an AI assistant (Claude); I reviewed and tested it.
🤖 Generated with Claude Code
Summary by CodeRabbit
idcolumn when a primary key is already available.