使用关系模型上的where子句获取数据

问题描述

在回送3.0中,我试图在关系上应用where子句时从多个关系模型中获取数据。

以下是模型的示例JSON:

modelA.json
{
    "properties": {
        "id": {
            "type": "string","required": true,"length": 20
        },"modelBid": {
            "type": "string"
        },...
    }
    "relations": {
        "modelB-rel": {
            "type": "belongsTo","model": "modelB","foreignKey": "modelBid","primaryKey": "id"
        },...
    }
}

modelB.json
{
    "properties": {
        "id": {
            "type": "string","required": "false","length": "20"
        },"name": {
            "type": "string","length": "255"
        },...
    }
}

我要执行的查询sql等效项:

SELECT * FROM modelA a 
LEFT JOIN modelB b
ON a.modelBid = b.id
WHERE b.name = 'abcd'

我正在使用的回送过滤器对象,该对象不提供获取预期结果的作用:

modelA.find({
    include: [
        {
            relation: 'modelB-rel',scope: {
                fields: ['id','name'],},}
    ],where: {
        modelB-rel: {
            name: 'abcd',}
}
...

请帮助我更正过滤器对象,以得到与上述sql等效的结果。

解决方法

要在相关模型上应用过滤器,必须将过滤器放入所包含模型的范围内。您可以使用以下内容:

modelA.find({
    include: [
        {
            relation: 'modelB-rel',scope: {
                fields: ['id','name'],where: {name: 'abcd'}          
            },}
    ],})