Generate Data Model does not emit primary_key: true for PostgreSQL tables with named PKs
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- postgresql, typescript
Research direction
Start in packages/cubejs-server-core/src/core/DevServer.ts by reading the /playground/db-schema and /playground/generate-schema handlers and their tablesSchema calls. Verify that generated PostgreSQL YAML includes primary_key: true for named primary-key columns and that cubes with joins compile without the reported error.
Written by the indexing model from the issue text.
Description
Bug Description
When using Generate Data Model in Cube Playground (v1.6.23, PostgreSQL), primary key columns are not marked with primary_key: true in the generated YAML. This causes compile errors for any cube that has joins defined:
salesorderheader cube: primary key for 'salesorderheader' is required when join is defined
salesorderdetail cube: primary key for 'salesorderdetail' is required when join is defined
... (dozens more)
Root Cause
DevServer.ts calls driver.tablesSchema() in both the /playground/db-schema and /playground/generate-schema endpoints:
// Line 141
const tablesSchema = await driver.tablesSchema();
// Line 179
const tablesSchema = req.body.tablesSchema || (await driver.tablesSchema());
tablesSchema() only queries information_schema.columns and does not fetch primary key constraints. As a result, no column ever gets attributes: ['primaryKey'], so ScaffoldingSchema.dimensions() never sets isPrimaryKey: true, and BaseSchemaFormatter never emits primary_key: true.
The correct method is tablesSchemaV2(), which calls primaryKeys() (overridden in PostgresDriver with a real information_schema.table_constraints query) and merges the results into the column data with attributes: ['primaryKey'].
Steps to Reproduce
- Connect Cube Playground to a PostgreSQL database (e.g. AdventureWorks)
- Go to Data Model → Generate Data Model
- Select any table with a join (e.g.
sales.salesorderheader) - Click Generate
- Observe the generated YAML has no
primary_key: truedimension - Cube fails to compile with:
primary key for 'X' is required when join is defined
Affected Tables / Columns
The bug affects all tables where the PK column name is not literally id. The scaffolding has a fallback heuristic fixCase(column.name) === 'id' but real-world tables use names like salesorderid, productid, businessentityid, etc.
Expected Behavior
Generated YAML should include the PK dimension marked as primary_key: true:
dimensions:
- name: salesorderid
sql: salesorderid
type: number
primary_key: true
public: false
Fix
In packages/cubejs-server-core/src/core/DevServer.ts, replace both calls to tablesSchema() 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());
Environment
- Cube version: 1.6.23
- Database: PostgreSQL 14.10
- Database: AdventureWorks (timchapman/postgresql-adventureworks)
- Dominant language
- Rust
- Stars
- 20.9k
- Forks
- 2.1k
- Avg merge
- 1d 31m
- Merged PRs (30d)
- 203
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cube-js/cube
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
gitbutlerapp/gitbutler#15998 · 1 comment ·
-
bug triage:deciding
Difficulty 1/5 Under an hour Newbie friendliness 88/100
open-telemetry/otel-arrow#4132 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100