首页 > 解决方案 > Loopback 4:如何结合查询父级和相关模型获取父级数据

问题描述

关系:客户有很多订单

Customer
id: number
name: string
Order
id: number
date: number

# assume relation on Customer with "orders" exists.
@belongsTo(Customer)
customerId: number

如何查询“%Kat%”之类的客户名称和介于(纪元)1579828913、1580828913 之间的订单日期。我正在尝试以下查询,但它没有给出所需的结果。

{
  "where": {
    "name": {
        "ilike": "%Kat%"
    }
  },
  "include": [
    {
      "relation": "orders",
      "scope": {
         "where": {
             "date": {
                "between": [1579828913, 1580828913]
             }
          }
       }
    }
  ]
}

标签: node.jsloopbackjsstronglooploopback4

解决方案


包含模型的当前限制是无法添加范围。在此处查看更多内容,其中说按父模型过滤: https ://loopback.io/doc/en/lb4/Relations.html#limitations 您必须手动完成,先找到客户,然后再找到订单。


推荐阅读