简体   繁体   English

Cube.js - 无法加入 2 个或更多表,但不断收到“找不到要加入的加入路径”错误

[英]Cube.js - Unable to join 2 or more tables but keep getting a "Can't find join path to join" error

I am new to Cube.js and I am trying to get data from 2 or more tables using the join.我是 Cube.js 的新手,我正在尝试使用连接从 2 个或更多表中获取数据。

The original SQL query would look like this which would join multiple tables together to display data from all 3.原始的 SQL 查询看起来像这样,它将多个表连接在一起以显示所有 3 个表的数据。

SELECT wo.EndDate AS DueDate, p.PurchaseNumber AS Details, [proc].Name
FROM WorkOrder wo
INNER JOIN Purchase p
ON wo.PurchaseId = p.Id
INNER JOIN Product pr
ON wo.ProductId = pr.Id
INNER JOIN [Procedure] [proc]
ON pr.ProcedureId = [proc].Id

Here is what I have for the Cube.js schema这是我对 Cube.js 模式的看法

cube(`WorkOrder`, {
    sql: `SELECT * FROM dbo."WorkOrder"`,

    joins: {
        Purchase: {
            relationship: `hasMany`,
            sql: `${WorkOrder}.PurchaseId = ${Purchase}.Id`
        },
        Product: {
            relationship:`hasMany`,
            sql: `${WorkOrder}.ProductId = ${Product.Id}`
        },
        Procedure: {
            relationship: `hasMany`,
            sql: `${Product}.ProcedureId = ${Procedure}.Id`
        }
    },

    dimensions: {
            id: {
                sql: `${CUBE}."Id"`,
                type: `number`,
                primaryKey: true,
                shown: true
            },

            scheduledstartdate: {
                sql: `${CUBE}."ScheduledStartDate"`,
                type: `time`
            },

            scheduledenddate: {
                sql: `${CUBE}."ScheduledEndDate"`,
                type: `time`
            }
    }
});

cube(`Purchase`, {
    sql: `SELECT * FROM dbo."Purchase"`,

    dimensions: {
        id: {
            sql: `${CUBE}."Id"`,
            type: `number`,
            primaryKey: true
        },

        customerpurchasenumber: {
            sql: `${CUBE}."CustomerPurchaseNumber"`,
            type: `string`
        }
    }
});

cube(`Product`, {
    sql: `SELECT * FROM dbo."Product"`,

    dimensions: {
        id: {
            sql: `${CUBE}."Id"`,
            type: `number`,
            primaryKey: true
        },

        name: {
            sql: `${CUBE}."Name"`,
            type: `string`
        }
    }
});

cube(`Procedure`, {
    sql: `SELECT * FROM dbo."Procedure"`,

    dimensions: {
        id: {
            sql: `${CUBE}."Id"`,
            type: `number`,
            primaryKey: true
        },

        name: {
            sql: `${CUBE}."Name"`,
            type: `string`
        },
    }
});

And then I would try to query in the Cube.js API with this然后我会尝试用这个在 Cube.js API 中查询

'{"dimensions":["WorkOrder.scheduledenddate", "Purchase.customerpurchasenumber", "Procedure.Name"]}'

But I would keep getting the "Can't find join path to join 'WorkOrder', 'Purchase'" error.但我会不断收到“找不到加入‘WorkOrder’、‘Purchase’的加入路径”错误。 I tried to make it easier and query only 2 tables, but still the same error.我试图让它更容易,只查询 2 个表,但仍然是同样的错误。 Can someone point out what I am missing to get this to work?有人可以指出我缺少什么才能让它发挥作用吗?

NOTE : There is now a more complete and correct answer in the official documentation .注意官方文档中现在有更完整和正确的答案

The "Can't find join path to join..." error occurs when the cube.js cannot find a way to build a join for the cubes passed in the query.当 cube.js 找不到为查询中传递的多维数据集构建连接的方法时,会出现“找不到连接路径以连接...”错误。

To fix the error, you need to describe the joins parameter要修复错误,您需要描述连接参数

Based on the relationships you described, cube.js builds a directed graph in which the nodes are cubes and the edges are the relationships you described.根据您描述的关系,cube.js 构建了一个有向图,其中节点是立方体,边是您描述的关系。

Since cube.js uses left join when joining cubes, the order of the cubes in the query is important.由于 cube.js 在连接多维数据集时使用左连接,因此查询中多维数据集的顺序很重要。

In your case, the graph looks like this:在您的情况下,图表如下所示:

在此处输入图像描述

So, your query is correct所以,你的查询是正确的

{"dimensions":["WorkOrder.scheduledenddate", "Purchase.customerpurchasenumber", "Procedure.Name"]}

But, for example, this query is wrong但是,例如,这个查询是错误的

{"dimensions":["Procedure.name","Purchase.customerpurchasenumber"]}

Since the graph does not have an edge outgoing from "Procedure" and leading to "Purchase"由于该图没有从“程序”传出并导致“购买”的边缘

在此处输入图像描述

To fix the error, add the joins parameter to the description of the "Procedure" cube, which describes the relationship between "Procedure" and "Purchase"要修复错误,请将 joins 参数添加到“Procedure”多维数据集的描述中,它描述了“Procedure”和“Purchase”之间的关系

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

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