'Loopback4 filter inside the scope return list still

I'm using lb4 I have some problems here, I try to find the list with where conditions inside the scope with pagination.

 const teacherWithStudents = await this.teacherRepository.find({limit:10,skip:0,
    include: [{
      relation: "student",
      scope: {
        where: { "name": "some random name here" },
      }
    }]
  })

The expected teacher's array is : [] (because I searched a random string in student name which is not in DB)

but I got teachers to array without student like this: [{teacherId:1,teacherName:"Stella"}{teacherId:2,teacherName:"Mery"}]

if I filter student names if no teacher has a student that I filtered I need an empty array but I get only a teacher.

I hope I explained the issue in detail. Thanks in advance



Solution 1:[1]

This is expected as the parent and relation queries should be perceived as two separate queries.

First, the list of teachers based on the parent query are resolved. The IDs of the resolved teachers are then used as a constraint when querying for the list of students.

Both results are then combined together to create the final response.

Solution 2:[2]

Loopback uses left join. if you want to find only teachers where student is not null then you have to use inner join with native query.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 Tayyab Gulzar