简体   繁体   English

如何在 Cube.js 中实现间接连接?

[英]How to achieve an indirect join in Cube.js?

I might misunderstand cube.js joins, but in SQL I can do a join from a table removed by one like so:我可能会误解cube.js连接,但在 SQL 中,我可以从被删除的表中进行连接,如下所示:

SELECT * FROM a
LEFT JOIN b ON a.name = b.name
LEFT JOIN c ON b.state = c.state;

My question is: how do I achieve this in cube.js ?我的问题是:如何在cube.js中实现这一点?

I tried to recreate above with the following schema (and some variations on it):我尝试使用以下架构(以及它的一些变体)重新创建上面的内容:

cube(`A`, {
  sql: `SELECT * FROM a`,

  joins: {
    B: {
      sql: `${A}.name = ${B}.name`,
      relationship: `belongsTo`,
    },
    C: {
      sql: `${B}.state = ${C}.state`,
      relationship: `belongsTo`,
    },
  },

  dimensions: {
    id: {
      sql: `id`,
      type: `number`,
      primaryKey: true,
    },

    name: {
      sql: `${B}.name`,
      type: `string`,
    },

    state: {
      sql: `${C}.state`,
      type: `string`,
    },
  },
});

where cubes B and C are also defined including the dimensions used here.其中立方体BC也被定义,包括此处使用的尺寸。

However, formulating the following query:但是,制定以下查询:

dimensions: [`A.state`];

throws the error:抛出错误:

Error: ER_BAD_FIELD_ERROR: Unknown column 'b.state' in 'on clause'

I might well miss the obvious, and would be grateful for any pointers...我很可能会错过明显的,并会感谢任何指示...

It turns out that the following B to C join:原来下面BC加入:

  joins: {

    //...

    C: {
      sql: `${B}.state = ${C}.state`,
      relationship: `belongsTo`,
    },
  },

needs to happen within cube B not cube A .需要在多维数据集B而不是多维数据集A内发生。 The reason seems to be that cubes reference other cubes rather than building persistent tables under the hood.原因似乎是多维数据集引用了其他多维数据集,而不是在底层构建持久表。

This also explains why dimensions that originate from joined tables still need to point to the cubes representing the joined table - like for example the state dimension in the question's code above:这也解释了为什么源自连接表的维度仍然需要指向代表连接表的多维数据集 - 例如上面问题代码中的state维度:

state: {
  sql: `${C}.state`,
  type: `string`,
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM