{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1025","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1025/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1025/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1025/events","html_url":"https://github.com/tgriesser/bookshelf/issues/1025","id":117010437,"number":1025,"title":".through() does not work as expected","user":{"login":"agarzola","id":439649,"avatar_url":"https://avatars.githubusercontent.com/u/439649?v=3","gravatar_id":"","url":"https://api.github.com/users/agarzola","html_url":"https://github.com/agarzola","followers_url":"https://api.github.com/users/agarzola/followers","following_url":"https://api.github.com/users/agarzola/following{/other_user}","gists_url":"https://api.github.com/users/agarzola/gists{/gist_id}","starred_url":"https://api.github.com/users/agarzola/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/agarzola/subscriptions","organizations_url":"https://api.github.com/users/agarzola/orgs","repos_url":"https://api.github.com/users/agarzola/repos","events_url":"https://api.github.com/users/agarzola/events{/privacy}","received_events_url":"https://api.github.com/users/agarzola/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-11-15T18:48:08Z","updated_at":"2015-11-15T19:00:12Z","closed_at":null,"body":"Here are my models:\r\n\r\n```javascript\r\n// models/Org.js\r\nvar Org = bookshelf.Model.extend({\r\n  tableName: 'orgs',\r\n  admins: function () {\r\n    return this.hasMany('User').through('Admin')\r\n  }\r\n})\r\n\r\nmodule.exports = bookshelf.model('Org', Org)\r\n```\r\n\r\n```javascript\r\n// models/User.js\r\nvar User = bookshelf.Model.extend({\r\n  tableName: 'users',\r\n  org: function () {\r\n    return this.belongsTo('Org').through('Admin', 'id')\r\n  }\r\n})\r\n\r\nmodule.exports = bookshelf.model('User', User)\r\n```\r\n\r\n```javascript\r\n// models/Admin.js\r\nvar Admin = bookshelf.Model.extend({\r\n  tableName: 'admins'\r\n})\r\n\r\nmodule.exports = bookshelf.model('Admin', Admin)\r\n```\r\n\r\nThe `admins` table referenced in the `models/Admin.js` model has a `user_id` column and an `org_id` column. When I invoke `Org.forge({ id: 1 }).fetch({ withRelated: ['admins'] })`, my expectation is that bookshelf will do the following:\r\n- query the `admins` table to find all rows with a value of `1` in the `org_id` field\r\n- query the `users` table to find all users with `id`s found in the `user_id` column of the rows found in the `admins` table\r\n- return all of rows found in the `users` table when I perform `org.related('admins').\r\n\r\nThis doesn’t seem to be the case, however; I get the following error message when I try the fetch statement above:\r\n\r\n> Error: select \"users\".\\*, \"admins\".\"id\" as \"_pivot_id\", \"admins\".\"farm_id\" as \"_pivot_farm_id\" from \"users\" inner join \"admins\" on \"admins\".\"id\" = \"users\".\"admin_id\" where \"admins\".\"farm_id\" in (1) - SQLITE_ERROR: no such column: **users.admin_id**\r\n\r\nThis is puzzling; why would it try to find `admin_id` inside the `users` table to begin with? Then I tried making the following change to the `Org` model:\r\n\r\n```javascript\r\nadmins: function () {\r\n  return this.hasMany('User').through('Admin', 'id')\r\n}\r\n```\r\n\r\nThis does not result in a formal error, but the resulting `admins` array is incorrectly populated. It returns a user model whose `id` is the same as the `org` I am fetching instead of the `user_id` in the `admins` table. Simply put, my org’s `id` is 1, and its admin (as per the only row in the `admins` table) should be user with `id` 2, but the user that gets put into my `org.admins` array is the user with `id` 1.\r\n\r\nAdding a third argument to the `.through()` method has no effect (as documented in issue #673) on the above results. Is this a bug, or am I doing something wrong?\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/156843401","html_url":"https://github.com/tgriesser/bookshelf/issues/1025#issuecomment-156843401","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/1025","id":156843401,"user":{"login":"agarzola","id":439649,"avatar_url":"https://avatars.githubusercontent.com/u/439649?v=3","gravatar_id":"","url":"https://api.github.com/users/agarzola","html_url":"https://github.com/agarzola","followers_url":"https://api.github.com/users/agarzola/followers","following_url":"https://api.github.com/users/agarzola/following{/other_user}","gists_url":"https://api.github.com/users/agarzola/gists{/gist_id}","starred_url":"https://api.github.com/users/agarzola/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/agarzola/subscriptions","organizations_url":"https://api.github.com/users/agarzola/orgs","repos_url":"https://api.github.com/users/agarzola/repos","events_url":"https://api.github.com/users/agarzola/events{/privacy}","received_events_url":"https://api.github.com/users/agarzola/received_events","type":"User","site_admin":false},"created_at":"2015-11-15T19:00:11Z","updated_at":"2015-11-15T19:00:11Z","body":"And when I fetch user with `id` 2, which does have a row in the `admins` table, no org is associated with it at all."}}