{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/3756","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/3756/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/3756/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/3756/events","html_url":"https://github.com/sequelize/sequelize/issues/3756","id":78231088,"number":3756,"title":"Model.scope ambiguous columns","user":{"login":"joshuadmatthews","id":6090341,"avatar_url":"https://avatars.githubusercontent.com/u/6090341?v=3","gravatar_id":"","url":"https://api.github.com/users/joshuadmatthews","html_url":"https://github.com/joshuadmatthews","followers_url":"https://api.github.com/users/joshuadmatthews/followers","following_url":"https://api.github.com/users/joshuadmatthews/following{/other_user}","gists_url":"https://api.github.com/users/joshuadmatthews/gists{/gist_id}","starred_url":"https://api.github.com/users/joshuadmatthews/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshuadmatthews/subscriptions","organizations_url":"https://api.github.com/users/joshuadmatthews/orgs","repos_url":"https://api.github.com/users/joshuadmatthews/repos","events_url":"https://api.github.com/users/joshuadmatthews/events{/privacy}","received_events_url":"https://api.github.com/users/joshuadmatthews/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-19T20:02:21Z","updated_at":"2015-05-19T20:55:35Z","closed_at":"2015-05-19T20:55:35Z","body":"This is a copy of issues #3123 which seems to not be fixed unless I am missing some new configuration to resolve it... I believe it was supposed to be fixed in the scope refactor.\r\n\r\nWhen a model has a scope that contains a where, it will cause all columns referencing that model in the SQL's where clause to be referenced without being prefixed by their table reference. If including an association that also has a column with the name referenced in the where clause, and the query does not subquery for the source model, the query will error due to ambiguous column names.\r\n\r\nExample\r\n\r\nHere's an example model definition.\r\n\r\nvar Foo = sequelize.define('Foo', {\r\n  status: DataTypes.STRING\r\n}, {\r\n  defaultScope: { where: { status: 'active' } }\r\n});\r\n\r\nvar Bar = sequelize.define('Bar');\r\n\r\nFoo.hasMany(Bar);\r\nBar.belongsTo(Foo);\r\n\r\nHere, we query for a specific Foo instance by id, which will cause Sequelize to generate a query that does not use a subquery to retrieve Foo, allowing the where conditions to break out into the main query.\r\n\r\nFoo.find({\r\n  where: { id: 1 } // This causes the source model (Foo) to not be retrieved via subquery\r\n  include: Bar\r\n});\r\nThe generated SQL's FROM will be Foo, it will JOIN Bar, and then the where clause will look something like\r\n\r\nWHERE `id` = 1 AND `status` = 'active'\r\n\r\nAnd this will result in the error Ambiguous column name 'id'."},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/103665288","html_url":"https://github.com/sequelize/sequelize/issues/3756#issuecomment-103665288","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/3756","id":103665288,"user":{"login":"janmeier","id":1210224,"avatar_url":"https://avatars.githubusercontent.com/u/1210224?v=3","gravatar_id":"","url":"https://api.github.com/users/janmeier","html_url":"https://github.com/janmeier","followers_url":"https://api.github.com/users/janmeier/followers","following_url":"https://api.github.com/users/janmeier/following{/other_user}","gists_url":"https://api.github.com/users/janmeier/gists{/gist_id}","starred_url":"https://api.github.com/users/janmeier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/janmeier/subscriptions","organizations_url":"https://api.github.com/users/janmeier/orgs","repos_url":"https://api.github.com/users/janmeier/repos","events_url":"https://api.github.com/users/janmeier/events{/privacy}","received_events_url":"https://api.github.com/users/janmeier/received_events","type":"User","site_admin":false},"created_at":"2015-05-19T20:55:35Z","updated_at":"2015-05-19T20:55:35Z","body":"The PR is merged into master.\r\n\r\nYour example works fine for me\r\n\r\n```sql\r\nSELECT `Foo`.`id`, `Foo`.`status`, `Foo`.`createdAt`, `Foo`.`updatedAt`, `Bars`.`id` AS `Bars.id`, `Bars`.`createdAt` AS `Bars.createdAt`, `Bars`.`updatedAt` AS `Bars.updatedAt`, `Bars`.`FooId` AS `Bars.FooId` FROM `Foos` AS `Foo` LEFT OUTER JOIN `Bars` AS `Bars` ON `Foo`.`id` = `Bars`.`FooId` WHERE `Foo`.`id` = 1 AND `Foo`.`status` = 'active';\r\n```\r\n\r\nPlease create a PR with a failing test case if you continue to have problems"}}