In mu situation a 'TopicColumn' hasAndBelongsToMany 'Topic', also, a 'Topic' hasAndBelongToMany 'TopicColumn'
My code is like
topics = yield Promise.promisify(column.topics)(filter); // filter has only 'include' and 'order'
pageCount = yield Promise.promisify(column.topics.count)();
console.log(pageCount)
console.log(topics.length)
the result is 3 and 6.(3 belongs to this column and 3 belongs to another, that makes 6)
@upupzealot I am trying to reproduce your bug and see if I can help. Could you fork this sandbox: https://github.com/strongloop/loopback-sandbox
and replace it with your code? Thanks!
@jannyHou will do that as soon as possible, thx
I stuck into the same problem, is there any update to this issue?
@Cyperwu Could you fork this sandbox: https://github.com/strongloop/loopback-sandbox
and replace it with your code? Thanks!
@jannyHou sure I'll try
@jannyHou I just realized that it could be my wrong understand of querying data.
Assume I have two models, Teacher and Student, they have a hasAndBelongsToMany relation.
So i do something like Teachers.Students.count({ where: { gender: male }}), will return 0
Is that a correct way to count all the male students belongs to this teacher?
It seems that no matter what I fill in the where filter(in the API explorer), it will always return 0.
hi @Cyperwu from your description, what you expect is correct, while I still prefer to see your app to debug more, because even a small config in relation could cause behaviors different.
And please double check the doc of hasAndBelongsToMany to make sure you set everything correctly, e.g. foreignKey: https://github.com/strongloop/loopback-datasource-juggler/blob/5bf18b07280e22d713e722b8e23f8aee14c6587e/lib/relations.js#L133-L154
BTW, are you using LB2.x or LB3.x? in 2.x when there is a through model, which is automatically set in hasAndBelongsToMany, the filter in api like teacher.students.count is applied on a through model, 3.x fixes that and I will back port that pr to 2.x asap.
hi @jannyHou I use 2.x. So It turns out I was counting through models! Seems that I shoud upgrade to 3.x. Thanks!
@Cyperwu no problem. Hope 3.x works for you. I am closing this issue since problem solved. If you still run into error, feel free to reopen it and I will continue working on it. Thanks for understanding.
Most helpful comment
@jannyHou will do that as soon as possible, thx