Skip to content

fix(devserver): use tablesSchemaV2 for Generate Data Model to include primary keys#10519

Open
Funbucket wants to merge 1 commit intocube-js:masterfrom
Funbucket:fix/generate-data-model-primary-key-detection
Open

fix(devserver): use tablesSchemaV2 for Generate Data Model to include primary keys#10519
Funbucket wants to merge 1 commit intocube-js:masterfrom
Funbucket:fix/generate-data-model-primary-key-detection

Conversation

@Funbucket
Copy link
Copy Markdown

Summary

Fixes #10517

When using Generate Data Model in Cube Playground, primary key columns are not marked with primary_key: true in the generated YAML, causing compile errors for cubes with joins defined.

Root Cause

DevServer.ts calls driver.tablesSchema() in both endpoints:

// /playground/db-schema
const tablesSchema = await driver.tablesSchema();

// /playground/generate-schema
const tablesSchema = req.body.tablesSchema || (await driver.tablesSchema());

tablesSchema() queries only information_schema.columns — it does not fetch primary key constraints. So no column gets attributes: ['primaryKey'], meaning ScaffoldingSchema never sets isPrimaryKey: true, and BaseSchemaFormatter never emits primary_key: true.

tablesSchemaV2() already exists and does the right thing: it calls primaryKeys() (implemented per-driver) and merges PK information into column attributes.

Fix

Replace both calls with tablesSchemaV2():

- const tablesSchema = await driver.tablesSchema();
+ const tablesSchema = await driver.tablesSchemaV2();
- const tablesSchema = req.body.tablesSchema || (await driver.tablesSchema());
+ const tablesSchema = req.body.tablesSchema || (await driver.tablesSchemaV2());

Impact

  • Affects all databases where the PK column is not literally named id (the only fallback heuristic in ScaffoldingSchema)
  • Reproduced with PostgreSQL 14 + AdventureWorks; any real-world schema with named PKs (salesorderid, productid, businessentityid, etc.) is affected

Test plan

  • Generate Data Model on a PostgreSQL table with a named PK (e.g. salesorderid) — generated YAML should include primary_key: true
  • Generated YAML compiles without "primary key is required" errors
  • Existing behaviour for tables with no joins is unchanged

… primary keys

tablesSchema() only queries information_schema.columns and does not fetch
primary key constraints. As a result, no column gets attributes: ['primaryKey'],
so the scaffolding never emits primary_key: true in generated YAML files.

Replace both calls in DevServer.ts (/playground/db-schema and
/playground/generate-schema) with tablesSchemaV2(), which calls primaryKeys()
(overridden in each driver with the appropriate query) and merges PK information
into column attributes before scaffolding runs.

Without this fix, any cube with joins defined fails to compile:
  "primary key for 'X' is required when join is defined in order to make
  aggregates work properly"

Fixes cube-js#10517
@Funbucket Funbucket requested a review from a team as a code owner March 18, 2026 08:51
@github-actions github-actions bot added javascript Pull requests that update Javascript code pr:community Contribution from Cube.js community members. labels Mar 18, 2026
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.

Generate Data Model does not emit primary_key: true for PostgreSQL tables with named PKs

1 participant