Skip to content

fix(server-core): use tablesSchemaV2 for Generate Data Model so PKs are detected - #11806

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

fix(server-core): use tablesSchemaV2 for Generate Data Model so PKs are detected#11806
haechangcho wants to merge 1 commit 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

#10517

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

/playground/db-schema and /playground/generate-schema in DevServer.ts both called 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.

Swapped both call sites to tablesSchemaV2() — a backward-compatible superset, no other behavior change.

Notes

  • driver is typed as BaseDriver (@cubejs-backend/query-orchestrator), which declares tablesSchemaV2() publicly, so no capability guard is needed.
  • No existing unit tests cover DevServer.ts's route 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant