Skip to content

ddl: fix erwin DDL import + scope SQL DDL export to one subject area - #66

Merged
dbose merged 2 commits into
mainfrom
fix-ddl-erwin-import
Nov 3, 2026
Merged

dbose merged 2 commits into
mainfrom
fix-ddl-erwin-import

Conversation

@dbose

@dbose dbose commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Two commits, both on the SQL-DDL interchange path that you're using to get models into erwin.

1. Fix the fatal that aborted erwin's DDL import

Exporting Modelith SQL DDL and running erwin's "Import from External Format" (DdlScript bridge) aborted with:

✔ Processed 'CREATE TABLE' statement for 'asset' (5 the recognized statement).
● java.lang.IllegalArgumentException: Illegal Argument: Cannot add the same Attribute to Key
● The Java process did not complete successfully

erwin was handed a PRIMARY KEY listing the same column twice (a pk key group with a duplicated member, seen in erwin-imported models), and that one fatal killed the whole import.

  • De-dupe key-column lists (order-preserving): PK members, the business-key fallback, and FK child columns. No more PRIMARY KEY (col, col) / FOREIGN KEY (col, col).
  • Default a FK's referenced columns to the parent's PK when the relationship doesn't name them, so REFERENCES points at a real key.

2. Scope the SQL DDL export to one subject area (no stubs, drop cross-area FKs)

Just like erwin's own subject-area export, a scoped SQL DDL export now emits only the selected area's tables.

  • A cross-area relationship is dropped, and the out-of-area neighbour (e.g. account) is not emitted as a stub table. The neighbours already exist in the target erwin mart, so the slice imports as an independent model and you re-establish the cross-area FKs by hand after merge. The FK column stays on the in-area table as an ordinary attribute.
  • mdl export sql --subject-area/-s <name|id>, /api/export/sql?subject_area=<id>, and the canvas Export menu scopes the SQL DDL link to a single active subject-area filter (same · filtered area only note as erwin).

This supersedes the "give each stub a PK so the FK attaches" idea from the other session's analysis — per your workflow, you want no stubs and will wire cross-area FKs yourself in the target model.

Verified

Whole-model vs scoped, live:

=== SCOPED: Trading ===
CREATE TABLE trade (
  trade_id INTEGER NOT NULL,
  account_id INTEGER NOT NULL,
  PRIMARY KEY (trade_id)
);

Only trade (no account stub), cross-area REFERENCES account dropped, account_id column retained.

Mermaid/DBML/CSV unchanged (whole-model). 433 passed, ruff clean; canvas tsc clean, Vitest 55 passed; canvas bundle rebuilt.

Honest limit

Not verified against real erwin here (no erwin in CI); built to the exact fatal in the import log. The real test is re-exporting DDL and re-running the erwin DdlScript import on Windows.

dbose added 2 commits November 3, 2026 11:03
…import)

Importing Modelith's SQL DDL into erwin aborted with
'java.lang.IllegalArgumentException: Cannot add the same Attribute to Key' on the first table
whose PRIMARY KEY listed the same column twice (seen in real erwin-imported models, where a pk
key group can carry a duplicated member). The fatal killed the whole import at that table.

- De-dupe key-column lists order-preservingly: PK members, business-key fallback, and FK child
  columns. No more PRIMARY KEY (col, col) / FOREIGN KEY (col, col).
- Default a FK's referenced columns to the PARENT's primary key when the relationship doesn't
  name them, so REFERENCES points at a real key. erwin otherwise drops the FK with 'there is
  not a primary or unique key in table <parent> that contains column ...' (a warning, not
  fatal). A parent with NO primary key at all still can't receive a FK -- that is a modelling
  condition the emitter cannot invent around.

Tests: a duplicated-PK-member model emits the column once; an FK with no named parent columns
REFERENCES the parent PK. 432 passed.
…rea FKs)

Like erwin's own subject-area export, a scoped SQL DDL export now emits ONLY the selected
subject area's tables. A relationship with an end outside the area is dropped and the
out-of-area neighbour is NOT emitted as a stub table — the neighbours already exist in the
target erwin model, so the slice imports as an independent model and the cross-area FKs are
re-established there by hand. The FK COLUMN stays on the in-area table as an ordinary attribute.

- build_tables(model, adapter, subject_area=None): filters to the area's logical entities;
  the existing child/parent lookup already drops a relationship whose other end is out of scope.
- emit_sql_ddl(..., subject_area=None) threads it through.
- CLI: mdl export sql --subject-area/-s <name|id>.
- API: /api/export/sql?subject_area=<id>.
- Canvas: the Export menu scopes the SQL DDL link to a single active subject-area filter, same
  as erwin (the '. filtered area only' note shows for both).

Mermaid/DBML/CSV are unchanged (they pass no subject_area -> whole model). Rebuilt canvas bundle.
Tests: scoped export emits only in-area tables, keeps within-area FKs, drops cross-area FKs with
no stub. 433 passed; canvas tsc clean, Vitest 55 passed.
@dbose dbose changed the title ddl: de-dupe key columns so erwin can import Modelith SQL DDL ddl: fix erwin DDL import + scope SQL DDL export to one subject area Nov 3, 2026
@dbose
dbose merged commit f22076e into main Nov 3, 2026
2 checks passed
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.

1 participant