Skip to content

Commit e242806

Browse files
authored
Update Direction-of-Joins.md
1 parent 4d0aa40 commit e242806

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

docs/Guides/Direction-of-Joins.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,91 @@ cube('Customers', {
129129
}
130130
});
131131
```
132+
133+
## Transitive Join Pitfall
134+
135+
Let's consider an example where we have many to many relationship between `A-C` through `B` cube:
136+
137+
```javascript
138+
cube(`A`, {
139+
// ...
140+
measures: {
141+
type: `count`
142+
}
143+
});
144+
145+
cube(`B`, {
146+
// ...
147+
joins: {
148+
A: {
149+
sql: `${B}.a_id = ${A}.id`,
150+
relationship: `hasMany`
151+
},
152+
C: {
153+
sql: `${B}.c_id = ${C}.id`,
154+
relationship: `hasMany`
155+
}
156+
}
157+
});
158+
159+
cube(`C`, {
160+
// ...
161+
dimensions: {
162+
category: {
163+
sql: `category`,
164+
type: `string`
165+
}
166+
}
167+
});
168+
```
169+
170+
And we want to build the query:
171+
172+
```javascript
173+
{
174+
measures: ['A.count'],
175+
dimensions: ['C.category']
176+
}
177+
```
178+
179+
You'll get an error: `Error: Can't find join path to join 'A', 'C'`.
180+
The problem is joins are directed and if we try to connect `A` and `C` there's no path from `A` to `C` or either from `C` to `A`.
181+
On possible solution is to move `A-B` join from `B` cube to `A`:
182+
183+
> **NOTE:** Moving the join affects semantics and results of a join which are discussed in previous section.
184+
185+
```javascript
186+
cube(`A`, {
187+
// ...
188+
joins: {
189+
B: {
190+
sql: `${B}.a_id = ${A}.id`,
191+
relationship: `hasMany`
192+
},
193+
},
194+
195+
measures: {
196+
type: `count`
197+
}
198+
});
199+
200+
cube(`B`, {
201+
// ...
202+
joins: {
203+
C: {
204+
sql: `${B}.c_id = ${C}.id`,
205+
relationship: `hasMany`
206+
}
207+
}
208+
});
209+
210+
cube(`C`, {
211+
// ...
212+
dimensions: {
213+
category: {
214+
sql: `category`,
215+
type: `string`
216+
}
217+
}
218+
});
219+
```

0 commit comments

Comments
 (0)