cube-js / cube-js/cube

missing FROM-clause entry for table when building a measure from joined data

Open
#8,495 4 comments 2 reactions 1 assignee Claimed by @paveltiunov View on GitHub
data modeling
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Describe the bug**
Hi there,
I have two cubes Orders and Items and I want to add a measure on Orders to calculate totalOrderPrice. But I get missing FROM-clause entry for table "orders_measure_join__orders"
Can anyone help me out here?

```
cube(`Orders`, {
sql: `SELECT * FROM purchase_order`,

joins: {
Items: {
sql: `${CUBE}.id = ${Items}.fk_purchase_order`,
relationship: `hasMany`,
},
},

measures: {
count: {
type: `count`,
drillMembers: [id, createdAt],
},
totalOrderPrice: {
sql: `${Items.price} * ${Items.quantity}`,
type: `sum`,
},
},

dimensions: {
id: {
sql: `id`,
type: `string`,
primaryKey: true,
},
createdAt: {
sql: `created_at`,
type: `time`,
},
},
});

cube(`Items`, {
sql: `SELECT * FROM purchase_order_item`,

dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true,
},
orderId: {
sql: `fk_purchase_order`,
type: `number`,
},
price: {
sql: `price_per_unit`,
type: `number`,
},
quantity: {
sql: `quantity`,
type: `number`,
},
},
});
```

![image](https://github.com/user-attachments/assets/e7acf24f-e3d2-4120-a903-86a255439761)

**To Reproduce**
Steps to reproduce the behavior:
@igorlukanin on the slack channel also provided a minimal example and generated sql

```
cube(`Orders`, {
sql: `
SELECT 1 AS id, '2024-01-01'::TIMESTAMP AS created_at UNION ALL
SELECT 2 AS id, '2024-01-02'::TIMESTAMP AS created_at
`,

joins: {
Items: {
sql: `${CUBE}.id = ${Items}.fk_purchase_order`,
relationship: `hasMany`,
},
},

measures: {
count: {
type: `count`,
drillMembers: [id, createdAt],
},
totalOrderPrice: {
sql: `${Items.price} * ${Items.quantity}`,
type: `sum`,
},
},

dimensions: {
id: {
sql: `id`,
type: `string`,
primaryKey: true,
},
createdAt: {
sql: `created_at`,
type: `time`,
},
},
});

cube(`Items`, {
sql: `
SELECT 11 AS id, 1 AS fk_purchase_order, 10 AS price_per_unit, 5 AS quantity UNION ALL
SELECT 12 AS id, 1 AS fk_purchase_order, 10 AS price_per_unit, 6 AS quantity UNION ALL
SELECT 13 AS id, 2 AS fk_purchase_order, 10 AS price_per_unit, 7 AS quantity UNION ALL
SELECT 14 AS id, 2 AS fk_purchase_order, 10 AS price_per_unit, 8 AS quantity
`,

dimensions: {
id: {
sql: `id`,
type: `number`,
primaryKey: true,
},
orderId: {
sql: `fk_purchase_order`,
type: `number`,
},
price: {
sql: `price_per_unit`,
type: `number`,
},
quantity: {
sql: `quantity`,
type: `number`,
},
},
});
```
![image](https://github.com/user-attachments/assets/a211a04b-6d6f-4a06-8b96-d8b401b47561)

**Expected behavior**
The join of the measures should be referencing the correct alias

**Version:**
0.35.56+ (didn't test lower versions)

**Additional context**
Here is the [slack convo](https://cube-js.slack.com/archives/C04KDTE2EF7/p1721040052856109)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.