{"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":10,"created_at":"2015-08-09T23:05:07Z","updated_at":"2015-08-14T14:41:26Z","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/131129884","html_url":"https://github.com/sequelize/sequelize/issues/4285#issuecomment-131129884","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/4285","id":131129884,"user":{"login":"newUserOfGit","id":7701669,"avatar_url":"https://avatars.githubusercontent.com/u/7701669?v=3","gravatar_id":"","url":"https://api.github.com/users/newUserOfGit","html_url":"https://github.com/newUserOfGit","followers_url":"https://api.github.com/users/newUserOfGit/followers","following_url":"https://api.github.com/users/newUserOfGit/following{/other_user}","gists_url":"https://api.github.com/users/newUserOfGit/gists{/gist_id}","starred_url":"https://api.github.com/users/newUserOfGit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/newUserOfGit/subscriptions","organizations_url":"https://api.github.com/users/newUserOfGit/orgs","repos_url":"https://api.github.com/users/newUserOfGit/repos","events_url":"https://api.github.com/users/newUserOfGit/events{/privacy}","received_events_url":"https://api.github.com/users/newUserOfGit/received_events","type":"User","site_admin":false},"created_at":"2015-08-14T14:41:26Z","updated_at":"2015-08-14T14:41:26Z","body":"### Main part of a problem query:\r\n\r\n```javascript\r\n   embed.db.Log.findAll({\r\n\r\n    include: [{\r\n      model: embed.db.User,\r\n    }, {\r\n      model: embed.db.Comment,\r\n\r\n      include: [{\r\n        model: embed.db.User,\r\n        },\r\n\r\n        // IF I DELETE THE NEXT INCLUDE, ERROR DISAPPEARS.\r\n        include:  [{\r\n          model: embed.db.Ban,\r\n          attributes: [...],\r\n        }],\r\n\r\n      }, \r\n\r\n    order: [['id', 'DESC']],\r\n  })\r\n```\r\n\r\n### Associations:\r\n\r\nLog:\r\n```javascript\r\n      models.Log.belongsTo(models.User, {\r\n        foreignKey: 'AdminId',\r\n      });\r\n      models.Log.belongsTo(models.Comment);\r\n```\r\n\r\nUser:\r\n```javascript\r\n      models.User.hasMany(models.Ban, { foreignKey: 'UserId', });\r\n      models.User.hasMany(models.Ban, { foreignKey: 'AdminId', });\r\n      models.User.hasMany(models.Comment);\r\n```\r\n\r\nComment:\r\n```javascript\r\n      models.Comment.belongsTo(models.User);\r\n      models.Comment.hasOne(models.Comment, {\r\n        as: 'Parent',\r\n        foreignKey: 'ParentId',\r\n      });\r\n      models.Comment.hasMany(models.Ban);\r\n```"}}