Skip to content

fix(er): draw SQLAlchemy primary and foreign keys in D2, PlantUML and DOT - #356

Merged
FROWNINGdev merged 1 commit into
FROWNINGdev:mainfrom
Xnadir:fix/er-sqlalchemy-keys-all-formats
Nov 24, 2026
Merged

FROWNINGdev merged 1 commit into
FROWNINGdev:mainfrom
Xnadir:fix/er-sqlalchemy-keys-all-formats

Conversation

@Xnadir

@Xnadir Xnadir commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

For a SQLAlchemy table, er --format d2, plantuml and dot marked only the first column of a composite primary key and drew each foreign key column as a ForeignKey row with no type. #210 fixed this for DBML only. Take this OrderLine, from the #210 tests, whose key (order_id, tenant_id) is also a foreign key to Order:

before after
D2 order_id: ForeignKey {constraint: foreign_key}; no primary key on OrderLine; Order.tenant_id not a key order_id: Integer {constraint: [primary_key; foreign_key]}, both tenant_id rows are keys
PlantUML * order_id : Integer, then order_id : ForeignKey again below the line * order_id : Integer <<FK>>, * tenant_id : Integer
DOT OrderLine has no PK row; order_id is ForeignKey · FK order_id is Integer · PK · FK, both tenant_id rows are PK

The DBML rule from #210 is now two helpers, _is_column() (a SQLAlchemy foreign key is a typed column; only relationship() attributes and Django relation fields are drawn as relation rows) and _primary_key_columns() (every primary_key=True column 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

  • Bug fix (non-breaking, restores expected behaviour)

Test plan

  • New test_composite_constraint_preserves_column_types_and_primary_keys_in_every_format in tests/test_sqlalchemy_parser.py builds 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 on main and passes with the fix.
  • cd cli && python -m pytest -q: 1359 passed, 6 skipped.
  • ruff check and mypy are clean on the changed files.
  • I also rendered a model with a plain ForeignKey("order.id") column in all four formats and compared them by eye.

Checklist

  • I ran the full test suite locally (cd cli && pytest -q for Python, npm test for TypeScript) and it is green
  • I added or updated tests that cover the change (bugfixes should get a regression test)
  • If the change is user-facing I updated the CHANGELOG under ## [Unreleased]
  • If the change touches the MCP tool contract (new tool, new arg, new error code) I updated the tool description in mcp_server.py and the relevant tests in test_mcp_server.py (not touched)
  • If this is a breaking change I called it out under ## Summary above 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

  • Bug Fixes
    • Corrected SQLAlchemy schema diagrams in DBML, D2, PlantUML, and DOT so foreign-key columns retain their data types and primary-key status.
    • Improved rendering of composite primary keys and columns that are both primary and foreign keys.
    • Avoided displaying a synthetic id column when a primary key is already available.

… 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]>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The serializers now use shared column and primary-key selection for SQLAlchemy models. They preserve foreign-key column types and render all declared primary-key columns, including composite keys. Tests check D2, PlantUML, and Graphviz output.

Changes: SQLAlchemy key rendering

Layer / File(s) Summary
Shared key selection and serializer output
cli/orm_lens/er_formats.py, cli/tests/test_sqlalchemy_parser.py, CHANGELOG.md
Shared helpers identify drawable columns and declared primary-key columns. DBML, D2, PlantUML, and Graphviz use these selections to render key constraints. Tests check composite foreign-key output in D2, PlantUML, and Graphviz. The changelog records the rendering changes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related PRs

  • FROWNINGdev/orm-lens#210: Adds the DBML behavior for retaining a composite foreign-key column’s scalar type and primary-key marker, which this PR extends to other formats.
  • FROWNINGdev/orm-lens#199: Introduces SQLAlchemy ER output for foreign keys, relationships, and primary keys, which this PR updates across formats.

Suggested reviewers: frowningdev, snehaapratap, jjordan0c

Merge Risk: Minimal · up to 5ab72

This change fixes how SQLAlchemy primary and foreign keys appear in D2, PlantUML and Graphviz diagrams. Foreign-key columns keep their types and all composite primary-key columns are marked. No actionable merge risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: fixing how SQLAlchemy primary keys and foreign keys are drawn in the D2, PlantUML and DOT ER formats. The raw summary and PR objectives confirm this, including the sha…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: fixing how SQLAlchemy primary keys and foreign keys are drawn in the D2, PlantUML and DOT ER formats. The raw summary and PR objectives confirm this, including the shared rules for composite primary keys and typed foreign-key columns. The title is specific and uses the conventional fix(scope) prefix. It does not cover the DBML or Django behavior, which is unchanged, and that is acceptable.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 77132042-d729-4d81-92c8-af3e0f5f29b1
Commits

Reviewing files that changed from the base of the PR and between b8b404d and 5ab7275.

Files selected for processing: 3

  • CHANGELOG.md
  • cli/orm_lens/er_formats.py
  • cli/tests/test_sqlalchemy_parser.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@FROWNINGdev
FROWNINGdev merged commit e82259b into FROWNINGdev:main Nov 24, 2026
20 checks passed
FROWNINGdev added a commit that referenced this pull request Nov 24, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants