【问题标题】:SequelizeJS: Recursive include same modelSequelizeJS:递归包含相同的模型
【发布时间】:2018-03-16 16:26:11
【问题描述】:

我想使用 SequelizeJS 递归地从 Postgres 数据库中获取包括所有关联模型的数据。

地理位置表

id | isoCode | name | parentId | type | updatedAt | deletedAt

parentId 持有父 GeoLocation 的 id。

模型关联

GeoLocation.belongsTo(GeoLocation, {
    foreignKey: 'parentId',
    as: 'GeoLocation'
});

数据说明及示例

GeoLocation 类型及其关系: city -> subdivision -> country -> continent

id: 552, name: Brooklyn, type: city, parentId: 551
id: 551, name: New York, type: subdivision, parentId: 28
id: 28, name: United States, type: country, parentId: 27
id: 27, name: North America, type: continent, parentId: NULL

现在,当查询一个城市时,只要设置了parentId,我希望包含所有关系。

GeoLocation.findOne({
    where: {
        id: 552
    },
    include: [
        {
            model: GeoLocation,
            as: 'GeoLocation',
            include: [
                {
                    model: GeoLocation,
                    as: 'GeoLocation',
                    include: [
                        {
                            model: GeoLocation,
                            as: 'GeoLocation',
                        }
                    ],
                }
            ],
        }
    ],
});

JSON 响应

{  
   "GeoLocation":{  
      "id":552,
      "type":"city",
      "name":"Brooklyn",
      "GeoLocation":{  
         "id":551,
         "type":"subdivision",
         "name":"New York",
         "GeoLocation":{  
            "id":28,
            "type":"country",
            "name":"United States",
            "GeoLocation":{  
               "id":27,
               "type":"continent",
               "name":"North America"
            }
         }
      }
   }
}

上述解决方案有效,但我有强烈的感觉,有更好的方法来做到这一点,而不必多次包含模型。我在文档中找不到任何相关内容。我错过了什么吗?

【问题讨论】:

  • 我正在寻找相同的东西,以及 MariaDB :) 如果您碰巧克服了这个问题,请分享您的解决方案 ^^
  • @Deunz Nah,我一直坚持到项目结束。但我仍然希望看到比这更好的解决方案 :)

标签: postgresql recursion sequelize.js


【解决方案1】:

我已经转向了不同的解决方案。我没有使用 sequelize 本机伪语言来实现这一点。因为显然,递归包含功能在续集中是不可能的。

这是我使用的递归模型:

我想要这个递归信息,哪个组织与哪个相关,无论我如何获得这个信息,我都会稍后处理它以使信息成形(树,平面列表,......等等)

我使用涉及递归 SQL 的 sequelize RAW 查询:

exports.getArborescenceByLienId = function (relationType, id) {
    const query = 'With cte as (\n' +
        '    select  id_organisationSource, \n' +
        '            relationType,\n' +
        '            id_organisationTarget\n' +
        '    from    (\n' +
        '                    select * from relation where relationType= :relationType\n'     +
        '                ) relations_sorted,\n' +
        '            (\n' +
        '                  select @pv := :orgId\n' +
        '                ) initialisation\n' +
        '    where   find_in_set(id_organisationSource, @pv)\n' +
        '    and     length(@pv := concat(@pv, \',\', id_organisationTarget))\n' +
        ')\n' +
        'select source.id as `idSource`, cte.relationType, target.id as `idTarget`\n' +
        'from cte \n' +
        'left outer join organisation source on cte.id_organisationSource = source.id\n' +
        'left outer join organisation target on cte.id_organisationTarget = target.id;';
    return models.sequelize.query(
        query,
        {
            type: models.sequelize.QueryTypes.SELECT,
            replacements: { orgId: id, relationType: relationType}
        }
    );
};

想象一下我有一个像这样的对象模型:

(假设这里的每个关系都有相同的类型:比如 isManaging) ID = 1 的查询结果将是:

ID = 2 的查询结果为:

然后我将这个平面列表转换为一棵树,然后瞧 :)

如果您对这个平面列表到树的转换算法感兴趣,请告诉我。

希望对您有所帮助。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-22
    • 2015-09-19
    • 2016-07-13
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2019-05-14
    相关资源
    最近更新 更多