{"action":"closed","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/5029","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/5029/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/5029/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/5029/events","html_url":"https://github.com/sequelize/sequelize/issues/5029","id":121934785,"number":5029,"title":"foreign key issues","user":{"login":"pensacola1989","id":485505,"avatar_url":"https://avatars.githubusercontent.com/u/485505?v=3","gravatar_id":"","url":"https://api.github.com/users/pensacola1989","html_url":"https://github.com/pensacola1989","followers_url":"https://api.github.com/users/pensacola1989/followers","following_url":"https://api.github.com/users/pensacola1989/following{/other_user}","gists_url":"https://api.github.com/users/pensacola1989/gists{/gist_id}","starred_url":"https://api.github.com/users/pensacola1989/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pensacola1989/subscriptions","organizations_url":"https://api.github.com/users/pensacola1989/orgs","repos_url":"https://api.github.com/users/pensacola1989/repos","events_url":"https://api.github.com/users/pensacola1989/events{/privacy}","received_events_url":"https://api.github.com/users/pensacola1989/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-12-13T18:14:48Z","updated_at":"2015-12-15T16:59:42Z","closed_at":"2015-12-15T16:59:42Z","body":"If overwirte foreign key,but don't remove the \"primarykey\" attribute on other field , overwrite don't affected\r\nuser.js\r\n```javascript\r\n  var User = sequelize.define(\"Users\", {\r\n      Id: {\r\n        type: DataTypes.INTEGER,\r\n        // primaryKey: true,\r\n        autoIncrement: true\r\n      },\r\n      openid: {\r\n        type: DataTypes.STRING,\r\n        primaryKey: true,\r\n        field: 'openid'\r\n      },\r\n      name: {\r\n        type: DataTypes.STRING,\r\n        field: 'name'\r\n      },\r\n      username: {\r\n        type: DataTypes.STRING,\r\n        field: 'username'\r\n      }},{\r\n        classMethods: {\r\n            associate: function(models) {\r\n                User.hasOne(models.Sign, { foreignKey: 'openid', targetKey: 'openid' });\r\n            }\r\n        });\r\n```\r\nsign.js\r\n\r\n```javascript\r\nvar Sign = sequelize.define('Sign', {\r\n    openid: {\r\n        type: DataTypes.STRING\r\n    },\r\n    id: {\r\n        allowNull: false,\r\n        autoIncrement: true,\r\n        primaryKey: true,\r\n        type: DataTypes.INTEGER\r\n    },\r\n    createdAt: {\r\n        allowNull: true,\r\n        type: DataTypes.DATE\r\n    },\r\n    meetingid: {\r\n        type: DataTypes.INTEGER,\r\n        allowNull: false,\r\n        defaultValue: -1\r\n    },\r\n    updatedAt: {\r\n        allowNull: false,\r\n        type: DataTypes.DATE,\r\n        defaultValue: DataTypes.NOW\r\n    }\r\n  }, {\r\n    classMethods: {\r\n      associate: function(models) {\r\n        Sign.belongsTo(models.Users, { foreignKey: 'openid' });\r\n      }\r\n    }\r\n  });\r\n```\r\n\r\nwhen I run:\r\n\r\n```javascript\r\nreturn UserModel.findOne({\r\n\t\twhere: { openid: openId },\r\n\t\tinclude: [{\r\n\t\t\tmodel: SignModel\r\n\t\t}]\r\n\t});\r\n```\r\n\r\nthe generated SQL is:\r\n\r\n```sql\r\nSELECT\r\n\t\"Users\".\"openid\",\r\n\t\"Users\".\"name\",\r\n\t\"Users\".\"username\",\r\n\t\r\n\t\"Sign\".\"openid\" AS \"Sign.openid\",\r\n\t\"Sign\".\"id\" AS \"Sign.id\",\r\n\t\"Sign\".\"createdAt\" AS \"Sign.createdAt\",\r\n\t\"Sign\".\"meetingid\" AS \"Sign.meetingid\",\r\n\t\"Sign\".\"updatedAt\" AS \"Sign.updatedAt\"\r\nFROM\r\n\t\"Users\" AS \"Users\"\r\nLEFT OUTER JOIN \"Signs\" AS \"Sign\" ON \"Users\".\"Id\" = \"Sign\".\"openid\"  -- Users.Id should be Users.openid\r\nLIMIT 1;\r\n\r\n```\r\nthe right SQL i need is\r\n```sql\r\nLEFT OUTER JOIN \"Signs\" AS \"Sign\" ON \"Users\".\"openid\" = \"Sign\".\"openid\"\r\n```\r\n\r\nthe point is ,*When I remove the 'primarykey attribute on user.js* everthing will be fine!\r\n\r\nI 'm so confused ,and waiting for explanation, Thanks "}}