{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/4605","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/4605/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/4605/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/4605/events","html_url":"https://github.com/sequelize/sequelize/issues/4605","id":109623672,"number":4605,"title":"using limit with hasMany throws an error","user":{"login":"Kiura","id":5522096,"avatar_url":"https://avatars.githubusercontent.com/u/5522096?v=3","gravatar_id":"","url":"https://api.github.com/users/Kiura","html_url":"https://github.com/Kiura","followers_url":"https://api.github.com/users/Kiura/followers","following_url":"https://api.github.com/users/Kiura/following{/other_user}","gists_url":"https://api.github.com/users/Kiura/gists{/gist_id}","starred_url":"https://api.github.com/users/Kiura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kiura/subscriptions","organizations_url":"https://api.github.com/users/Kiura/orgs","repos_url":"https://api.github.com/users/Kiura/repos","events_url":"https://api.github.com/users/Kiura/events{/privacy}","received_events_url":"https://api.github.com/users/Kiura/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-10-03T13:40:22Z","updated_at":"2015-10-03T15:38:45Z","closed_at":null,"body":"error: missing FROM-clause entry for table \"userGroups.groupMessages\".\r\n\r\nRelationships:\r\n```javascript\r\nm.group.hasMany(m.message, {as:'groupMessages', foreignKey: 'groupMessageId', onDelete: 'SET NULL' });\r\nm.group.belongsToMany(m.user, {through: 'userGroup', as: 'groupMembers', foreignKey: 'groupId', onDelete: 'CASCADE' });\r\nm.user.belongsToMany(m.group, {through: 'userGroup', as: 'userGroups', foreignKey: 'userId', onDelete: 'CASCADE' });\r\nm.user.hasMany(m.message, {as: 'userMessages', foreignKey: 'senderId', onDelete: 'SET NULL' });\r\nm.message.hasOne(m.userMessage, { foreignKey: 'messageId', as:'messageState', onDelete: 'SET NULL' });\r\n```\r\nThe query:\r\n```javascript\r\nm.user.find({ \r\n    where: {id: data.id},\r\n    attributes: ['id'],\r\n    order: [ [ {model: m.group, as:'userGroups'},\r\n            { model: m.message, as: 'groupMessages' }, 'createdAt', 'DESC' ] ],\r\n    include: [ {\r\n        model: m.group, as: 'userGroups', \r\n        through: {attributes: []},\r\n        attributes: ['id','name', 'avatar'],\r\n        include: [ {\r\n            model: m.message, as: 'groupMessages',\r\n            attributes: ['id','message', 'type', 'thumb', 'createdAt','senderId'],\r\n            include: [ {\r\n                model: m.userMessage, as: 'messageState',\r\n                attributes: ['id','state', 'messageId', 'recieverId', 'createdAt']\r\n            }],\r\n            separate:true,\r\n            limit:1\r\n        }],\r\n    }]\r\n});\r\n```\r\n```SQL\r\nSELECT \"user\".\"id\", \r\n       \"usergroups\".\"id\"     AS \"userGroups.id\", \r\n       \"usergroups\".\"name\"   AS \"userGroups.name\", \r\n       \"usergroups\".\"avatar\" AS \"userGroups.avatar\" \r\nFROM   \"users\" AS \"user\" \r\n       LEFT OUTER JOIN (\"usergroups\" AS \"userGroups.userGroup\" \r\n                        INNER JOIN \"groups\" AS \"userGroups\" \r\n                                ON \"usergroups\".\"id\" = \r\n       \"userGroups.userGroup\".\"groupid\") \r\n                    ON \"user\".\"id\" = \"userGroups.userGroup\".\"userid\" \r\nWHERE  \"user\".\"id\" = '2' \r\nORDER  BY \"usergroups.groupmessages\".\"createdAt\" DESC; \r\n```\r\nP.S. if `limit:1` is removed - no errors, but it returns all messages..."},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/145258864","html_url":"https://github.com/sequelize/sequelize/issues/4605#issuecomment-145258864","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/4605","id":145258864,"user":{"login":"Kiura","id":5522096,"avatar_url":"https://avatars.githubusercontent.com/u/5522096?v=3","gravatar_id":"","url":"https://api.github.com/users/Kiura","html_url":"https://github.com/Kiura","followers_url":"https://api.github.com/users/Kiura/followers","following_url":"https://api.github.com/users/Kiura/following{/other_user}","gists_url":"https://api.github.com/users/Kiura/gists{/gist_id}","starred_url":"https://api.github.com/users/Kiura/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Kiura/subscriptions","organizations_url":"https://api.github.com/users/Kiura/orgs","repos_url":"https://api.github.com/users/Kiura/repos","events_url":"https://api.github.com/users/Kiura/events{/privacy}","received_events_url":"https://api.github.com/users/Kiura/received_events","type":"User","site_admin":false},"created_at":"2015-10-03T15:38:45Z","updated_at":"2015-10-03T15:38:45Z","body":"I used `limit:1`, so it would be a separate query, otherwise it throws an error\r\n"}}