{"action":"closed","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/3125","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/3125/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/3125/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/3125/events","html_url":"https://github.com/sequelize/sequelize/issues/3125","id":57584328,"number":3125,"title":"Facing problem in Find query on objects with Many to many relation","user":{"login":"sahilgarg25","id":4540933,"avatar_url":"https://avatars.githubusercontent.com/u/4540933?v=3","gravatar_id":"","url":"https://api.github.com/users/sahilgarg25","html_url":"https://github.com/sahilgarg25","followers_url":"https://api.github.com/users/sahilgarg25/followers","following_url":"https://api.github.com/users/sahilgarg25/following{/other_user}","gists_url":"https://api.github.com/users/sahilgarg25/gists{/gist_id}","starred_url":"https://api.github.com/users/sahilgarg25/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sahilgarg25/subscriptions","organizations_url":"https://api.github.com/users/sahilgarg25/orgs","repos_url":"https://api.github.com/users/sahilgarg25/repos","events_url":"https://api.github.com/users/sahilgarg25/events{/privacy}","received_events_url":"https://api.github.com/users/sahilgarg25/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-02-13T10:45:15Z","updated_at":"2015-02-13T12:59:39Z","closed_at":"2015-02-13T12:59:39Z","body":"\r\nI have below 3 models\r\n```javascript\r\nmodule.exports = function(sequelize, DataTypes) {\r\n    var CATEGORY = 'category';\r\n    var termTaxonomy = sequelize.define(\"termTaxonomy\", {\r\n        termTaxonomyId: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'term_taxonomy_id',\r\n            primaryKey: true\r\n        },\r\n        taxonomy: DataTypes.STRING,\r\n        description: DataTypes.TEXT,\r\n        parent: DataTypes.BIGINT,\r\n        count: DataTypes.BIGINT\r\n    }, {\r\n        tableName: 'wp_term_taxonomy',\r\n        timestamps: false,\r\n        classMethods: {\r\n            associate: function(models) {\r\n                termTaxonomy.belongsTo(models.term, {\r\n                    foreignKey: 'term_id'\r\n                });\r\n                termTaxonomy.hasMany(models.post, {\r\n                    through: 'wp_term_relationships',\r\n                    foreignKey: 'term_taxonomy_id'\r\n                });\r\n                models.post.hasMany(models.termTaxonomy, {\r\n                    through: 'wp_term_relationships',\r\n                    foreignKey: 'object_id'\r\n                });\r\n            },\r\n            getCategoryPostsById: function(models, id) {\r\n                return termTaxonomy.find({\r\n                    include: [models.term, models.post],\r\n                    where: {\r\n                        termTaxonomyId: id,\r\n                        taxonomy: CATEGORY\r\n                    }\r\n                });\r\n            }\r\n        }\r\n    });\r\n    return termTaxonomy;\r\n};\r\n\r\nmodule.exports = function(sequelize, DataTypes) {\r\n    var termRelationship = sequelize.define(\"termRelationship\", {\r\n        objectId: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'object_id',\r\n            primaryKey: true\r\n        },\r\n        termTaxonomyId: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'term_taxonomy_id',\r\n            primaryKey: true\r\n        },\r\n        termOrder: {\r\n            type: DataTypes.INTEGER,\r\n            field: 'term_order'\r\n        }\r\n    }, {\r\n        tableName: 'wp_term_relationships',\r\n        timestamps: false\r\n    });\r\n    return termRelationship;\r\n};\r\n\r\nmodule.exports = function(sequelize, DataTypes) {\r\n    var post = sequelize.define(\"post\", {\r\n        id: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'ID',\r\n            primaryKey: true\r\n        },\r\n        author: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'post_author'\r\n        },\r\n        date: {\r\n            type: DataTypes.DATE,\r\n            field: 'post_date'\r\n        },\r\n        dateGMT: {\r\n            type: DataTypes.DATE,\r\n            field: 'post_date_gmt'\r\n        },\r\n        content: {\r\n            type: DataTypes.TEXT,\r\n            field: 'post_content'\r\n        },\r\n        title: {\r\n            type: DataTypes.TEXT,\r\n            field: 'post_title'\r\n        },\r\n        excerpt: {\r\n            type: DataTypes.TEXT,\r\n            field: 'post_excerpt'\r\n        },\r\n        status: {\r\n            type: DataTypes.STRING,\r\n            field: 'post_status'\r\n        },\r\n        commentStatus: {\r\n            type: DataTypes.STRING,\r\n            field: 'comment_status'\r\n        },\r\n        pingStatus: {\r\n            type: DataTypes.STRING,\r\n            field: 'ping_status'\r\n        },\r\n        password: {\r\n            type: DataTypes.STRING,\r\n            field: 'post_password'\r\n        },\r\n        name: {\r\n            type: DataTypes.STRING,\r\n            field: 'post_name'\r\n        },\r\n        toPing: {\r\n            type: DataTypes.TEXT,\r\n            field: 'to_ping'\r\n        },\r\n        pinged: {\r\n            type: DataTypes.TEXT,\r\n            field: 'pinged'\r\n        },\r\n        modified: {\r\n            type: DataTypes.DATE,\r\n            field: 'post_modified'\r\n        },\r\n        modifiedGMT: {\r\n            type: DataTypes.DATE,\r\n            field: 'post_modified_gmt'\r\n        },\r\n        contentFiltered: {\r\n            type: DataTypes.TEXT,\r\n            field: 'post_content_filtered'\r\n        },\r\n        parent: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'post_parent'\r\n        },\r\n        guid: {\r\n            type: DataTypes.STRING,\r\n            field: 'guid'\r\n        },\r\n        menuOrder: {\r\n            type: DataTypes.INTEGER,\r\n            field: 'menu_order'\r\n        },\r\n        type: {\r\n            type: DataTypes.STRING,\r\n            field: 'post_type'\r\n        },\r\n        mimeType: {\r\n            type: DataTypes.STRING,\r\n            field: 'post_mime_type'\r\n        },\r\n        commentCount: {\r\n            type: DataTypes.BIGINT,\r\n            field: 'comment_count'\r\n        }\r\n    }, {\r\n        tableName: 'wp_posts',\r\n        timestamps: false\r\n    });\r\n    return post;\r\n};\r\n```\r\n\r\nNow if I call the function \r\nmodels.termTaxonomy.getCategoryPostsById(models, 16);\r\n\r\nBelow Query gets executed:\r\nSELECT \r\n`termTaxonomy`.`term_taxonomy_id` AS `termTaxonomyId`, \r\n`termTaxonomy`.`taxonomy`, \r\n`termTaxonomy`.`description`, \r\n`termTaxonomy`.`parent`, \r\n`termTaxonomy`.`count`, \r\n`termTaxonomy`.`term_id`, \r\n`term`.`term_id` AS `term.termId`, \r\n`term`.`name` AS `term.name`, \r\n`term`.`slug` AS `term.slug`, \r\n`term`.`term_group` AS `term.termGroup`,\r\n`posts`.`ID` AS `posts.id`, \r\n`posts`.`post_author` AS `posts.author`, \r\n`posts`.`post_date` AS `posts.date`, \r\n`posts`.`post_date_gmt` AS `posts.dateGMT`, \r\n`posts`.`post_content` AS `posts.content`, \r\n`posts`.`post_title` AS `posts.title`, \r\n`posts`.`post_excerpt` AS `posts.excerpt`, \r\n`posts`.`post_status` AS `posts.status`, \r\n`posts`.`comment_status` AS `posts.commentStatus`, \r\n`posts`.`ping_status` AS `posts.pingStatus`, \r\n`posts`.`post_password` AS `posts.password`, \r\n`posts`.`post_name` AS `posts.name`, \r\n`posts`.`to_ping` AS `posts.toPing`, \r\n`posts`.`pinged` AS `posts.pinged`, \r\n`posts`.`post_modified` AS `posts.modified`, \r\n`posts`.`post_modified_gmt` AS `posts.modifiedGMT`, \r\n`posts`.`post_content_filtered` AS `posts.contentFiltered`, \r\n`posts`.`post_parent` AS `posts.parent`, \r\n`posts`.`guid` AS `posts.guid`, \r\n`posts`.`menu_order` AS `posts.menuOrder`, \r\n`posts`.`post_type` AS `posts.type`, \r\n`posts`.`post_mime_type` AS `posts.mimeType`, \r\n`posts`.`comment_count` AS `posts.commentCount`, \r\n`posts.wp_term_relationship`.`object_id` AS `posts.wp_term_relationship.object_id`, \r\n`posts.wp_term_relationship`.`term_taxonomy_id` AS `posts.wp_term_relationship.term_taxonomy_id` \r\nFROM `wp_term_taxonomy` AS `termTaxonomy` \r\nLEFT OUTER JOIN `wp_terms` AS `term` \r\n\tON `term`.`term_id` = `termTaxonomy`.`term_id`\r\nLEFT OUTER JOIN (`wp_term_relationships` AS `posts.wp_term_relationship` \r\n\t\t\t\t\tLEFT OUTER JOIN `wp_posts` AS `posts` \r\n\t\t\t\t\t\tON `posts`.`id` = `posts.wp_term_relationship`.`object_id`) \r\n\tON `termTaxonomy`.`termTaxonomyId` = `posts.wp_term_relationship`.`term_taxonomy_id` \r\nWHERE `termTaxonomy`.`taxonomy`='category' AND `termTaxonomy`.`term_taxonomy_id`='16';\r\n\r\nThis query throws an error:\r\nError Code: 1054. Unknown column 'termTaxonomy.termTaxonomyId' in 'on clause'\r\n\r\nHow ever if i change the query this column name to \r\n'termTaxonomy.term_taxonomy_id'\r\nThe query runs successfully.\r\n\r\nWhat changes do i need to make in the models so that the correct query is generated automatically. \r\n"}}