{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/7255","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/7255/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/7255/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/7255/events","html_url":"https://github.com/sequelize/sequelize/issues/7255","id":207736233,"number":7255,"title":"hasMany relationship with sourceKey specified does not seem to work. defaults to primary key of source table","user":{"login":"stringbeans","id":1228699,"avatar_url":"https://avatars2.githubusercontent.com/u/1228699?v=4","gravatar_id":"","url":"https://api.github.com/users/stringbeans","html_url":"https://github.com/stringbeans","followers_url":"https://api.github.com/users/stringbeans/followers","following_url":"https://api.github.com/users/stringbeans/following{/other_user}","gists_url":"https://api.github.com/users/stringbeans/gists{/gist_id}","starred_url":"https://api.github.com/users/stringbeans/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stringbeans/subscriptions","organizations_url":"https://api.github.com/users/stringbeans/orgs","repos_url":"https://api.github.com/users/stringbeans/repos","events_url":"https://api.github.com/users/stringbeans/events{/privacy}","received_events_url":"https://api.github.com/users/stringbeans/received_events","type":"User","site_admin":false},"labels":[{"id":636646701,"url":"https://api.github.com/repos/sequelize/sequelize/labels/stale","name":"stale","color":"aaaaaa","default":false},{"id":96184457,"url":"https://api.github.com/repos/sequelize/sequelize/labels/waiting","name":"waiting","color":"fad8c7","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-02-15T08:23:32Z","updated_at":"2017-09-07T01:08:32Z","closed_at":"2017-07-25T21:47:10Z","author_association":"NONE","body":"## What you are doing?\r\nI'm trying to create a hasMany relationship between Country and TranslationEntry. Here is my code below:\r\n\r\n```js\r\nsequelize.define('Country', {\r\n    countryId: {\r\n      type: DataTypes.INTEGER(10),\r\n      allowNull: false,\r\n      primaryKey: true,\r\n      autoIncrement: true,\r\n      field: 'country_id'\r\n    },\r\n    code: {\r\n      type: DataTypes.CHAR(2),\r\n      allowNull: false,\r\n      defaultValue: '',\r\n      field: 'code'\r\n    },\r\n    nameTranslationId: {\r\n      type: DataTypes.INTEGER(11),\r\n      allowNull: true,\r\n      field: 'name_translation_id'\r\n    }\r\n  })\r\n\r\nsequelize.define('TranslationEntry', {\r\n    translationId: {\r\n      type: DataTypes.INTEGER(10),\r\n      allowNull: false,\r\n      primaryKey: true,\r\n      field: 'translation_id'\r\n    },\r\n    languageId: {\r\n      type: DataTypes.INTEGER(3),\r\n      allowNull: false,\r\n      primaryKey: true,\r\n      references: {\r\n        model: 'language',\r\n        key: 'language_id'\r\n      },\r\n      field: 'language_id'\r\n    },\r\n    translation: {\r\n      type: DataTypes.TEXT,\r\n      allowNull: true,\r\n      field: 'translation'\r\n    }\r\n})\r\n\r\nmodels.Country.hasMany(models.TranslationEntry, {\r\n  as: 'nameTranslationEntries',\r\n  sourceKey: 'nameTranslationId',\r\n  foreignKey: 'translationId'\r\n})\r\n```\r\n\r\nAnd I'm querying by doing the following:\r\n\r\n```js\r\nCountry.find({\r\n            where: {\r\n              code: 'ca'\r\n            },\r\n            include: [{\r\n              model: this.DataAccessor.models.TranslationEntry,\r\n              as: 'nameTranslationEntries'\r\n            }]\r\n          })\r\n```\r\n\r\n## What do you expect to happen?\r\nI would expect the generated query to join on Country.name_translation_id = TranslationEntry.translation_id\r\n\r\n## What is actually happening?\r\nViewing the query logs, I am seeing it is attempting to join based on Country.country_id = TranslationEntry.translation_id\r\n\r\n__Dialect:__ mariadb\r\n__Database version:__ `mysql  Ver 15.1 Distrib 10.0.24-MariaDB, for osx10.11 (x86_64) using readline 5.1`\r\n__Sequelize version:__ `3.30.2`\r\n"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/327653687","html_url":"https://github.com/sequelize/sequelize/issues/7255#issuecomment-327653687","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/7255","id":327653687,"user":{"login":"johnny-dash","id":13574694,"avatar_url":"https://avatars3.githubusercontent.com/u/13574694?v=4","gravatar_id":"","url":"https://api.github.com/users/johnny-dash","html_url":"https://github.com/johnny-dash","followers_url":"https://api.github.com/users/johnny-dash/followers","following_url":"https://api.github.com/users/johnny-dash/following{/other_user}","gists_url":"https://api.github.com/users/johnny-dash/gists{/gist_id}","starred_url":"https://api.github.com/users/johnny-dash/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johnny-dash/subscriptions","organizations_url":"https://api.github.com/users/johnny-dash/orgs","repos_url":"https://api.github.com/users/johnny-dash/repos","events_url":"https://api.github.com/users/johnny-dash/events{/privacy}","received_events_url":"https://api.github.com/users/johnny-dash/received_events","type":"User","site_admin":false},"created_at":"2017-09-07T01:08:32Z","updated_at":"2017-09-07T01:08:32Z","author_association":"NONE","body":"I have the same issue, even I define the sourceKey, it still refer the primary key in source object"}}
{"actor":{"display_login":"johnny-dash"}}