Skip to content

fix: use tablesSchemaV2 for Generate Data Model so PKs are detected (Playground + CLI) - #11806

Open
haechangcho wants to merge 2 commits into
cube-js:masterfrom
haechangcho:fix/generate-data-model-primary-key-detection
Open

haechangcho wants to merge 2 commits into
cube-js:masterfrom
haechangcho:fix/generate-data-model-primary-key-detection

Conversation

@haechangcho

@haechangcho haechangcho commented Sep 9, 2026

Copy link
Copy Markdown

Check List

  • Tests have been run in packages where changes have been made if available
  • Linter has been run for changed code
  • Tests for the changes have been added if not covered yet
  • Docs have been added / updated if required

Issue Reference this PR resolves

Fixes #10517, Fixes #11270

Description of Changes Made (if issue reference is not provided)

Three call sites feed ScaffoldingSchema/ScaffoldingTemplate with driver.tablesSchema(), which only queries information_schema.columns and never sets attributes. ScaffoldingSchema.dimensions() decides isPrimaryKey purely from column.attributes?.includes('primaryKey'), so Generate Data Model never emitted primary_key: true unless a table's PK column happened to be named id. Any generated cube that also had a join then failed to compile with primary key for 'X' is required when join is defined.

BaseDriver.tablesSchemaV2() already exists for exactly this (added in #8115) — it calls tablesSchema() and merges in primaryKeys()/foreignKeys(), returning the same shape with attributes/foreign_keys added. It was defined but never actually called from any production code path; only a unit test exercised the PK-consuming logic directly with hand-built attributes data, so the missing wiring went unnoticed.

@paveltiunov independently reproduced and diagnosed this exact bug in #11270, tracing it down to informationColumnsSchemaReducer() always producing attributes: [] and confirming tablesSchemaV2() as the fix — this PR is that fix, plus a third call site the analysis there flagged but that wasn't covered yet:

  • packages/cubejs-server-core/src/core/DevServer.ts/playground/db-schema and /playground/generate-schema (first commit)
  • packages/cubejs-cli/src/command/generate.ts — the cubejs generate -t ... CLI command (second commit)

All three swapped to tablesSchemaV2() — a backward-compatible superset, no other behavior change.

Notes

  • driver is typed as BaseDriver in all three call sites, which declares tablesSchemaV2() publicly, so no capability guard is needed.
  • No existing unit tests cover DevServer.ts's or the CLI command's wiring directly; the PK-detection logic itself (attributes.includes('primaryKey')isPrimaryKey) is already covered by scaffolding-schema.test.ts / scaffolding-template.test.ts.

…re detected

/playground/db-schema and /playground/generate-schema both called
driver.tablesSchema(), which only queries information_schema.columns and
never merges primary/foreign key metadata. ScaffoldingSchema only marks a
dimension as a primary key via column.attributes.includes('primaryKey'),
which tablesSchema() never sets — so Generate Data Model never emits
primary_key: true for any table whose PK column isn't literally named `id`.
Any generated cube that also has a join then fails to compile with
"primary key for 'X' is required when join is defined".

driver.tablesSchemaV2() (BaseDriver) already exists for exactly this: it
calls tablesSchema() and merges in primaryKeys()/foreignKeys(), returning
the same DatabaseStructure shape with attributes/foreign_keys added where
applicable. Swapping both call sites to it is a backward-compatible
superset with no other behavior change.

Fixes cube-js#10517
@haechangcho
haechangcho requested a review from a team as a code owner September 9, 2026 06:09
@github-actions github-actions Bot added javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members. labels Sep 9, 2026
@haechangcho

haechangcho commented Sep 9, 2026

Copy link
Copy Markdown
Author

@paveltiunov — since you've already looked closely at the manufacturing-KPI schema issue we filed (#11680), flagging this one too: this is a small, self-contained fix (2 lines) for #10517.

Would appreciate a workflow approval so CI can actually run, and a look when you have a moment. Happy to make any changes requested.

The `cubejs generate -t ...` CLI command has the same bug as the
Playground endpoints fixed in the previous commit: it calls
driver.tablesSchema() and feeds the result into ScaffoldingTemplate,
which never sets a column's primaryKey attribute, so generated cubes
never get `primary_key: true` unless the PK column is literally named
`id`.

@paveltiunov already diagnosed this exact call site (and this exact
fix) in cube-js#11270.

Fixes cube-js#11270
@github-actions github-actions Bot added the backend:cli Issues relating to the CLI utility label Sep 14, 2026
@haechangcho haechangcho changed the title fix(server-core): use tablesSchemaV2 for Generate Data Model so PKs are detected fix: use tablesSchemaV2 for Generate Data Model so PKs are detected (Playground + CLI) Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:cli Issues relating to the CLI utility javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members.

Projects

None yet

1 participant