Skip to content

Querying associated model with scope and limit #1744

Description

@geekguy

Here is the test project: https://github.com/geekguy/loopback-sandbox (Details in readme)

Lets say we have 3 models, M1, M2 and M3 and instances are m1, m2 and m3.

M1 hasMany M2 and M2 hasMany M3.

Now. I want M2s for a given M1 and I want to include M3 with some condition.

m1.m2s({
  include: [
    {
      relation: 'm3s',
      scope: {
        where: {
          someCondition: something
        }
      }
    }
  ]
})

This returns all m2s and includes m3s with matching condition which is perfect.

Now I wan only one m3 per m2. So I ll add limit: 1

m1.m2s({
  include: [
    {
      relation: 'm3s',
      scope: {
        where: {
          someCondition: something,
          limit: 1
        }
      }
    }
  ]
})

But this call has some issues. It returns 1 m3 for first m2 and 0 for rest of them.
If I change limit: 2, it returns 1 m3 for first two m2 and 0 for rest of them.

Looks like scope condition is going wrong.

Please correct me if I missed anything or doing something wrong.

https://docs.strongloop.com/display/public/LB/Querying+related+models#Queryingrelatedmodels-Usingfiltersparameterswithincludedrelations

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions