【问题标题】:How to do where condition on include relation in Loopback如何在环回中包含关系的条件
【发布时间】:2020-11-06 01:12:26
【问题描述】:

我想通过包含模型的 where 条件获得包含关系的结果。

return this.htcApi.find({
  include: [
    {
      relation: 'nmos',
      scope: {
        include: 'countries',
      },
    },
  ],
  where: { name:'Welcome', "nmos.name":'agile'}
});

此where 是htc 模型名称的条件工作,不适用于noms 模块。

我想要像这样的查询

Select htc.*, nmos.* FROM htc LEFT JOIN nmos ON nmos.id = htc.n_id where htc.name = 'abc' and nmos.name = 'abc';

如何在“关系”表上添加 where 条件?

【问题讨论】:

    标签: mysql node.js loopbackjs loopback


    【解决方案1】:

    在您的查询中,您只需在scope 属性中添加属性where,如下所示:

    return this.htcApi.find({
      include: [
        {
          relation: 'nmos',
          scope: {
            include: 'countries',
            where: {
              and [
                { id: htc.n_id },
                { name: 'abc' },
              ],
            },
          },
        },
      ],
      where: { name: 'abc' }
    });
    

    这应该返回名为“abc”的htcApi 对象以及名称为“abc”和ID 为“n_id”的相关nmos 对象。

    【讨论】:

    • 我假设您在提出请求之前已经拥有它!确实它没有在提供的代码中定义
    【解决方案2】:

    您只需在 'include' 对象内的 'scope' 对象中添加 where 子句。所以代码会是这样的:

        return this.htcApi.find({
        include: [
          {
            relation: 'nmos',
            scope: {
              include: 'countries',
              where:{name:'agile'}
            },
          },
        ],
        where: { name:'Welcome'}
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      相关资源
      最近更新 更多