{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/4285","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/4285/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/4285/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/4285/events","html_url":"https://github.com/sequelize/sequelize/issues/4285","id":99938468,"number":4285,"title":"Model.findAll with includes breaks in version 3.5.x - Not unique table/alias","user":{"login":"barmatz","id":775026,"avatar_url":"https://avatars.githubusercontent.com/u/775026?v=3","gravatar_id":"","url":"https://api.github.com/users/barmatz","html_url":"https://github.com/barmatz","followers_url":"https://api.github.com/users/barmatz/followers","following_url":"https://api.github.com/users/barmatz/following{/other_user}","gists_url":"https://api.github.com/users/barmatz/gists{/gist_id}","starred_url":"https://api.github.com/users/barmatz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/barmatz/subscriptions","organizations_url":"https://api.github.com/users/barmatz/orgs","repos_url":"https://api.github.com/users/barmatz/repos","events_url":"https://api.github.com/users/barmatz/events{/privacy}","received_events_url":"https://api.github.com/users/barmatz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":11,"created_at":"2015-08-09T23:05:07Z","updated_at":"2015-08-14T14:53:08Z","closed_at":null,"body":"Forgive me if this has already been addressed.\r\nI have been developing a project on version 3.3.x, but when I upgraded it to 3.5.x it broke.\r\n\r\nA `findAll` query on my Transaction model that has includes returns a SQL error: `Error Code: 1066. Not unique table/alias: 'destinationAccount.currency'`.\r\n\r\nThe `findAll` call:\r\n```\r\nTransaction.findAll({\r\n  where: where,\r\n  attributes: [ 'id', 'status', 'quantity', 'createdAt', 'exchangeRate' ],\r\n  order: [ [ 'createdAt', 'DESC' ] ],\r\n  include: [{\r\n    model: Account,\r\n    as: 'sourceAccount',\r\n    attributes: [ 'id', 'balance', 'sortCode', 'number' ],\r\n    include: [{ \r\n      model: Currency,\r\n      as: 'currency',\r\n      attributes: [ 'id', 'code' ]\r\n    }]\r\n  }, {\r\n    model: Account,\r\n    as: 'destinationAccount',\r\n    attributes: [ 'id', 'balance', 'sortCode', 'number' ],\r\n    include: [{ \r\n      model: Currency,\r\n      as: 'currency',\r\n      attributes: [ 'id', 'code' ]\r\n    }]\r\n  }]\r\n})\r\n```\r\n\r\nRelevant associations:\r\n```\r\nTransaction.belongsTo(Account, { foreignKey: 'sourceAccountId', as: 'sourceAccount' });\r\nTransaction.belongsTo(Account, { foreignKey: 'destinationAccountId', as: 'destinationAccount' });\r\n```\r\n\r\nThe query generated is different depending on the version even though the options provided to the method are the same.\r\nIn v3.3.x line 26 is\r\n    '`Currencies` AS `sourceAccount.currency` ON `sourceAccount`.`currencyId` = `sourceAccount.currency`.`id`'\r\nIn v3.5.x line 26 is\r\n    ``Currencies` AS `destinationAccount.currency` ON `destinationAccount`.`currencyId` = `destinationAccount.currency`.`id``\r\n\r\n3.3.x query:\r\n```\r\nSELECT \r\n    `Transaction`.`id`,\r\n    `Transaction`.`status`,\r\n    `Transaction`.`quantity`,\r\n    `Transaction`.`createdAt`,\r\n    `Transaction`.`exchangeRate`,\r\n    `sourceAccount`.`id` AS `sourceAccount.id`,\r\n    `sourceAccount`.`balance` AS `sourceAccount.balance`,\r\n    `sourceAccount`.`sortCode` AS `sourceAccount.sortCode`,\r\n    `sourceAccount`.`number` AS `sourceAccount.number`,\r\n    `sourceAccount.currency`.`id` AS `sourceAccount.currency.id`,\r\n    `sourceAccount.currency`.`code` AS `sourceAccount.currency.code`,\r\n    `destinationAccount`.`id` AS `destinationAccount.id`,\r\n    `destinationAccount`.`balance` AS `destinationAccount.balance`,\r\n    `destinationAccount`.`sortCode` AS `destinationAccount.sortCode`,\r\n    `destinationAccount`.`number` AS `destinationAccount.number`,\r\n    `destinationAccount.currency`.`id` AS `destinationAccount.currency.id`,\r\n    `destinationAccount.currency`.`code` AS `destinationAccount.currency.code`\r\nFROM\r\n    `Transactions` AS `Transaction`\r\n        LEFT OUTER JOIN\r\n    `Accounts` AS `sourceAccount` ON `Transaction`.`sourceAccountId` = `sourceAccount`.`id`\r\n        LEFT OUTER JOIN\r\n    `Currencies` AS `sourceAccount.currency` ON `sourceAccount`.`currencyId` = `sourceAccount.currency`.`id`\r\n        LEFT OUTER JOIN\r\n    `Accounts` AS `destinationAccount` ON `Transaction`.`destinationAccountId` = `destinationAccount`.`id`\r\n        LEFT OUTER JOIN\r\n    `Currencies` AS `destinationAccount.currency` ON `destinationAccount`.`currencyId` = `destinationAccount.currency`.`id`\r\nORDER BY `Transaction`.`createdAt` DESC;\r\n```\r\n\r\n3.5.x query:\r\n```\r\nSELECT \r\n    `Transaction`.`id`,\r\n    `Transaction`.`status`,\r\n    `Transaction`.`quantity`,\r\n    `Transaction`.`createdAt`,\r\n    `Transaction`.`exchangeRate`,\r\n    `Transaction`.`btcBuyRate`,\r\n    `Transaction`.`btcSellRate`,\r\n    `sourceAccount`.`id` AS `sourceAccount.id`,\r\n    `sourceAccount`.`balance` AS `sourceAccount.balance`,\r\n    `sourceAccount`.`sortCode` AS `sourceAccount.sortCode`,\r\n    `sourceAccount`.`number` AS `sourceAccount.number`,\r\n    `sourceAccount.currency`.`id` AS `sourceAccount.currency.id`,\r\n    `sourceAccount.currency`.`code` AS `sourceAccount.currency.code`,\r\n    `destinationAccount`.`id` AS `destinationAccount.id`,\r\n    `destinationAccount`.`balance` AS `destinationAccount.balance`,\r\n    `destinationAccount`.`sortCode` AS `destinationAccount.sortCode`,\r\n    `destinationAccount`.`number` AS `destinationAccount.number`,\r\n    `destinationAccount.currency`.`id` AS `destinationAccount.currency.id`,\r\n    `destinationAccount.currency`.`code` AS `destinationAccount.currency.code`\r\nFROM\r\n    `Transactions` AS `Transaction`\r\n        LEFT OUTER JOIN\r\n    `Accounts` AS `sourceAccount` ON `Transaction`.`sourceAccountId` = `sourceAccount`.`id`\r\n        LEFT OUTER JOIN\r\n    `Currencies` AS `destinationAccount.currency` ON `destinationAccount`.`currencyId` = `destinationAccount.currency`.`id`\r\n        LEFT OUTER JOIN\r\n    `Accounts` AS `destinationAccount` ON `Transaction`.`destinationAccountId` = `destinationAccount`.`id`\r\n        LEFT OUTER JOIN\r\n    `Currencies` AS `destinationAccount.currency` ON `destinationAccount`.`currencyId` = `destinationAccount.currency`.`id`\r\nORDER BY `Transaction`.`createdAt` DESC;\r\n```"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/131134944","html_url":"https://github.com/sequelize/sequelize/issues/4285#issuecomment-131134944","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/4285","id":131134944,"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-08-14T14:53:08Z","updated_at":"2015-08-14T14:53:08Z","body":"@newUserOfGit Seems to work fine for me -  I assume Ban is a sub-include of User?\r\n\r\nI'm not sure we're getting anywhere by you posting small snippets, I'm sorry. Everything I've seen so far on this issue has been working fine for me. \r\n\r\nIf you have time, please fork the project and submit a pull request with a failing test, then we can work from there"}}