Skip to content

fix(core): parse exported JSON back with refs and table groups - #1073

Open
lucasarano wants to merge 1 commit into
holistics:masterfrom
lucasarano:claude/fix-514-json-schema-refs
Open

lucasarano wants to merge 1 commit into
holistics:masterfrom
lucasarano:claude/fix-514-json-schema-refs

Conversation

@lucasarano

Copy link
Copy Markdown

Exporting a database to JSON and parsing that JSON again failed as soon as the model had a ref or a table group, with errors like Can't find table "null"."posts".

The JSON export nests tables, refs and table groups inside each schema. When the Database constructor built those nested schemas, each Schema created its own refs and table groups inside its constructor, before the schema had been added to database.schemas. Looking up the endpoint tables then went through findOrCreateSchema, which did not see the schema being built and returned a new empty one, so the table was never found. I changed processSchemas to register every schema first and then create the nested enums, tables, refs, deps and table groups in the same order the top-level elements already use. That also covers a ref pointing to a table in a schema that comes later in the list.

Table groups had a second problem: TableGroup.export() writes each member as { tableName, schemaName }, but the constructor only read name. It now falls back to tableName, so the exported shape is accepted without changing the JSON output.

I added a round trip test in model_exporter.spec.ts that parses DBML with a same-schema ref, a cross-schema ref and a table group, exports it to JSON, parses the JSON and checks the DBML export matches the original. It fails on master and passes with this change.

Fixes #514

I made this fix with Claude Code. Happy to change anything.

Exporting a database to JSON and parsing that JSON again failed as soon as the model had a ref or a table group, with errors like `Can't find table "null"."posts"`.

The JSON export nests tables, refs and table groups inside each schema. When the Database constructor built those nested schemas, each Schema created its own refs and table groups inside its constructor, before the schema had been added to `database.schemas`. Looking up the endpoint tables then went through `findOrCreateSchema`, which did not see the schema being built and returned a new empty one, so the table was never found. I changed `processSchemas` to register every schema first and then create the nested enums, tables, refs, deps and table groups in the same order the top-level elements already use. That also covers a ref pointing to a table in a schema that comes later in the list.

Table groups had a second problem: `TableGroup.export()` writes each member as `{ tableName, schemaName }`, but the constructor only read `name`. It now falls back to `tableName`, so the exported shape is accepted without changing the JSON output.

I added a round trip test in model_exporter.spec.ts that parses DBML with a same-schema ref, a cross-schema ref and a table group, exports it to JSON, parses the JSON and checks the DBML export matches the original. It fails on master and passes with this change.
Copilot AI balanced review requested due to automatic review settings December 5, 2026 20:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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.

Generate DBML from JS object

3 participants