Count issue with related models using though model

未关闭
#1,795 4 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@regevbr 已经在做这个了。

开始于 2020年1月21日。

  • #1801 来自 @regevbr —— 已关闭,未合并

评估

这个 Issue 还没有评估数据。

描述

bug community-contribution good first issue LB3-only

Steps to reproduce

Create a model called Book with the properties id,author,title
Add a hasMany relation between the default User model and the Book model using a through Model called UserToBook which has the following properties: userId,bookId

The relation is defined in the User model like so.

    "books": {
      "type": "hasMany",
      "model": "Book",
      "foreignKey": "userId",
      "through": "UserToBook",
      "keyThrough": "bookId"
    },

Current Behavior

Querying the user for his books using a where filter works as expected (there is a bug there as well which I will address in a different issue). The reason it works is the related function
https://github.com/strongloop/loopback-datasource-juggler/blob/d54d76947724d75a0f46b151c064742c678b0bbe/lib/scope.js#L61
Which, after querying the through model, also queries the related model directly.

When trying to count the number of books of a user, using a where filter (on the author for example), the result is the entire number of books the user has and not the number of books from that author. The reason for that is that the count function
https://github.com/strongloop/loopback-datasource-juggler/blob/d54d76947724d75a0f46b151c064742c678b0bbe/lib/scope.js#L509-L528
Only queries the though model, and not the related model directly.

Expected Behavior

Return the actual count, taking the filter under account

Link to reproduction sandbox

Working on it

Additional information

It seems that the findOne,updateAll and destroyAll suffer from the same issue

主要语言
JavaScript
星标
279
派生
362
平均合并
21 小时 14 分钟
30 天内合并 PR
16

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

loopbackio/loopback-datasource-juggler 的其他 Issue

查看 loopbackio/loopback-datasource-juggler 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。