【问题标题】:Loopback query with where inside include包含 where 的环回查询
【发布时间】:2019-02-20 22:11:35
【问题描述】:

我正在尝试通过以下方式查询我的数据:

const filter = {where: {activity_id: activity_id, include: {relation: 'planGoal', scope: {where: {activity_goal_id: goal_id}}}}};

但这似乎不起作用,仅应用 activity_id 过滤器并返回错误数据。

所以我的问题是如何查询包含中的数据?甚至有可能吗?

供参考,以下是有问题的模型:

    {
  "name": "plan_goal_has_action_option",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "activity_id": {
      "type": "number"
    },
    "plan_has_goal_id": {
      "type": "number"
    },
    "action_option": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "planGoal": {
      "type": "belongsTo",
      "model": "plan_has_goal",
      "foreignKey": "plan_has_goal_id"
    }
  },
  "acls": [],
  "methods": {}
}


    {
  "name": "plan_has_goal",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "activity_id": {
      "type": "number"
    },
    "activity_goal_id": {
      "type": "number"
    }
  },
  "validations": [],
  "relations": {
    "goal": {
      "type": "belongsTo",
      "model": "activity_goal",
      "foreignKey": "activity_goal_id"
    },
    "actions": {
      "type": "hasMany",
      "model": "plan_goal_has_action_option",
      "foreignKey": "plan_has_goal_id"
    }
  },
  "acls": [],
  "methods": {}
}

【问题讨论】:

    标签: javascript loopbackjs


    【解决方案1】:

    includewhere 是两个不同的过滤器:

    {  
      where: {
        activity_id: activity_id
      },
      include: {  
        relation: 'planGoal',
        scope: {
          where: {
            activity_goal_id: goal_id
          }
        }
      }
    }

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-13
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多