{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/655","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/655/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/655/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/655/events","html_url":"https://github.com/tgriesser/bookshelf/issues/655","id":61138527,"number":655,"title":"How query a object through relations","user":{"login":"rkmax","id":395707,"avatar_url":"https://avatars.githubusercontent.com/u/395707?v=3","gravatar_id":"","url":"https://api.github.com/users/rkmax","html_url":"https://github.com/rkmax","followers_url":"https://api.github.com/users/rkmax/followers","following_url":"https://api.github.com/users/rkmax/following{/other_user}","gists_url":"https://api.github.com/users/rkmax/gists{/gist_id}","starred_url":"https://api.github.com/users/rkmax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rkmax/subscriptions","organizations_url":"https://api.github.com/users/rkmax/orgs","repos_url":"https://api.github.com/users/rkmax/repos","events_url":"https://api.github.com/users/rkmax/events{/privacy}","received_events_url":"https://api.github.com/users/rkmax/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-03-13T17:03:14Z","updated_at":"2015-03-18T21:38:49Z","closed_at":"2015-03-18T20:04:56Z","body":"I can't find examples, i trying to do\r\n\r\nGet all books with topics \"adventure\"\r\n\r\nI have 3 models, books, tags and topics, books has a relation many-to-many with topics and many-to-many with tags\r\n\r\n```js\r\nvar Book = bookshelf.Model.extend({\r\n    tags: function () {\r\n        return this.belongsToMany(Tag);\r\n    },\r\n\r\n    topics: function(){\r\n        return this.belongsToMany(Tag);\r\n    }\r\n});\r\n\r\nvar Books =  Models.Bookshelf.Collection.extend({model: Book});\r\n\r\nvar Tag = bookshelf.Model.extend({\r\n    books: function () {\r\n        return this.belongsToMany(Book);\r\n    }\r\n});\r\n\r\nvar Topic = bookshelf.Model.extend({\r\n    books: function () {\r\n        return this.belongsToMany(Book);\r\n    }\r\n});\r\n```\r\n\r\nSo i need do something like\r\n\r\n    \r\n    // GET /books?topic=\"adventure\"\r\n    var query = {where: {type: 'book'}};\r\n\r\n    // this queries are optional\r\n    if (req.query.topic || req.query.tag) {\r\n        query.andWhere = function(qb) {\r\n            if (req.query.topic) {\r\n                qb.orWhere('topics.name', '=', req.query.topic);\r\n            }\r\n            if (req.query.tag) {\r\n                qb.orWhere('tags.name', '=', req.query.tag);\r\n            }\r\n        };\r\n    }\r\n\r\n    Books.query(query)\r\n        .fetch({withRelated: ['topics', 'tags']})\r\n        .then(function(books) {\r\n            res.json(books);\r\n        })\r\n        .catch(function(err) {\r\n            console.log(err.stack);\r\n            res.status(500);\r\n            res.json(err);\r\n        });\r\n\r\n\r\nbut the generate SQL is wrong\r\n\r\n    'select \"books\".* from \"books\" where \"type\" = $1 and (\"topics\".\"name\" = $2)'\r\n\r\n**How i can add the required join table to this query?**"},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/83192051","html_url":"https://github.com/tgriesser/bookshelf/issues/655#issuecomment-83192051","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/655","id":83192051,"user":{"login":"bendrucker","id":808808,"avatar_url":"https://avatars.githubusercontent.com/u/808808?v=3","gravatar_id":"","url":"https://api.github.com/users/bendrucker","html_url":"https://github.com/bendrucker","followers_url":"https://api.github.com/users/bendrucker/followers","following_url":"https://api.github.com/users/bendrucker/following{/other_user}","gists_url":"https://api.github.com/users/bendrucker/gists{/gist_id}","starred_url":"https://api.github.com/users/bendrucker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bendrucker/subscriptions","organizations_url":"https://api.github.com/users/bendrucker/orgs","repos_url":"https://api.github.com/users/bendrucker/repos","events_url":"https://api.github.com/users/bendrucker/events{/privacy}","received_events_url":"https://api.github.com/users/bendrucker/received_events","type":"User","site_admin":false},"created_at":"2015-03-18T21:38:49Z","updated_at":"2015-03-18T21:38:49Z","body":"Referring to rkmax here. You're welcome to open a new issue with a full reproduction of your schema, models, the call you're trying to make, and the final response you'd like to send to the client."}}