{"action":"created","issue":{"url":"https://api.github.com/repos/strongloop/loopback/issues/1744","repository_url":"https://api.github.com/repos/strongloop/loopback","labels_url":"https://api.github.com/repos/strongloop/loopback/issues/1744/labels{/name}","comments_url":"https://api.github.com/repos/strongloop/loopback/issues/1744/comments","events_url":"https://api.github.com/repos/strongloop/loopback/issues/1744/events","html_url":"https://github.com/strongloop/loopback/issues/1744","id":112201867,"number":1744,"title":"Querying associated model with scope and limit","user":{"login":"geekguy","id":3037036,"avatar_url":"https://avatars.githubusercontent.com/u/3037036?v=3","gravatar_id":"","url":"https://api.github.com/users/geekguy","html_url":"https://github.com/geekguy","followers_url":"https://api.github.com/users/geekguy/followers","following_url":"https://api.github.com/users/geekguy/following{/other_user}","gists_url":"https://api.github.com/users/geekguy/gists{/gist_id}","starred_url":"https://api.github.com/users/geekguy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/geekguy/subscriptions","organizations_url":"https://api.github.com/users/geekguy/orgs","repos_url":"https://api.github.com/users/geekguy/repos","events_url":"https://api.github.com/users/geekguy/events{/privacy}","received_events_url":"https://api.github.com/users/geekguy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/strongloop/loopback/labels/bug","name":"bug","color":"e11d21"},{"url":"https://api.github.com/repos/strongloop/loopback/labels/major","name":"major","color":"eb6420"}],"state":"closed","locked":false,"assignee":{"login":"superkhau","id":1617364,"avatar_url":"https://avatars.githubusercontent.com/u/1617364?v=3","gravatar_id":"","url":"https://api.github.com/users/superkhau","html_url":"https://github.com/superkhau","followers_url":"https://api.github.com/users/superkhau/followers","following_url":"https://api.github.com/users/superkhau/following{/other_user}","gists_url":"https://api.github.com/users/superkhau/gists{/gist_id}","starred_url":"https://api.github.com/users/superkhau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/superkhau/subscriptions","organizations_url":"https://api.github.com/users/superkhau/orgs","repos_url":"https://api.github.com/users/superkhau/repos","events_url":"https://api.github.com/users/superkhau/events{/privacy}","received_events_url":"https://api.github.com/users/superkhau/received_events","type":"User","site_admin":false},"milestone":null,"comments":19,"created_at":"2015-10-19T18:12:38Z","updated_at":"2016-04-12T21:20:51Z","closed_at":"2016-04-06T21:05:17Z","body":"Here is the test project: https://github.com/geekguy/loopback-sandbox (Details in readme)\r\n\r\nLets say we have 3 models, M1, M2 and M3 and instances are m1, m2 and m3.\r\n\r\nM1 hasMany M2 and M2 hasMany M3.\r\n\r\nNow. I want M2s for a given M1 and I want to include M3 with some condition. \r\n\r\n```\r\nm1.m2s({\r\n  include: [\r\n    {\r\n      relation: 'm3s',\r\n      scope: {\r\n        where: {\r\n          someCondition: something\r\n        }\r\n      }\r\n    }\r\n  ]\r\n})\r\n```\r\n\r\nThis returns all m2s and includes m3s with matching condition which is perfect.\r\n\r\nNow I wan only one m3 per m2. So I ll add `limit: 1`\r\n\r\n```\r\nm1.m2s({\r\n  include: [\r\n    {\r\n      relation: 'm3s',\r\n      scope: {\r\n        where: {\r\n          someCondition: something,\r\n          limit: 1\r\n        }\r\n      }\r\n    }\r\n  ]\r\n})\r\n```\r\n\r\nBut this call has some issues. It returns 1 m3 for first m2 and 0 for rest of them.\r\nIf I change `limit: 2`, it returns 1 m3 for first two m2 and 0 for rest of them.\r\n\r\nLooks like scope condition is going wrong. \r\n\r\nPlease correct me if I missed anything or doing something wrong.\r\n\r\nhttps://docs.strongloop.com/display/public/LB/Querying+related+models#Queryingrelatedmodels-Usingfiltersparameterswithincludedrelations"},"comment":{"url":"https://api.github.com/repos/strongloop/loopback/issues/comments/209107812","html_url":"https://github.com/strongloop/loopback/issues/1744#issuecomment-209107812","issue_url":"https://api.github.com/repos/strongloop/loopback/issues/1744","id":209107812,"user":{"login":"superkhau","id":1617364,"avatar_url":"https://avatars.githubusercontent.com/u/1617364?v=3","gravatar_id":"","url":"https://api.github.com/users/superkhau","html_url":"https://github.com/superkhau","followers_url":"https://api.github.com/users/superkhau/followers","following_url":"https://api.github.com/users/superkhau/following{/other_user}","gists_url":"https://api.github.com/users/superkhau/gists{/gist_id}","starred_url":"https://api.github.com/users/superkhau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/superkhau/subscriptions","organizations_url":"https://api.github.com/users/superkhau/orgs","repos_url":"https://api.github.com/users/superkhau/repos","events_url":"https://api.github.com/users/superkhau/events{/privacy}","received_events_url":"https://api.github.com/users/superkhau/received_events","type":"User","site_admin":false},"created_at":"2016-04-12T21:20:51Z","updated_at":"2016-04-12T21:20:51Z","body":"@ebarault I merged in https://github.com/strongloop/loopback-datasource-juggler/issues/786. See if this is good enough for you."}}