loopbackio / loopbackio/loopback-datasource-juggler

Count issue with related models using though model

Abierto
#1,795 4 comentarios 0 reacciones 1 asignado Ver en GitHub

@regevbr ya está trabajando en esto.

Desde el 21/1/2020.

bug community-contribution good first issue LB3-only
Lenguaje dominante
JavaScript
Estrellas
282
Forks
366
Merge medio
17 h 4 min
PR fusionados (30 d)
10

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.