{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/6188","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/6188/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/6188/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/6188/events","html_url":"https://github.com/sequelize/sequelize/issues/6188","id":163116757,"number":6188,"title":"Composite primary key with associations","user":{"login":"jaumard","id":2970449,"avatar_url":"https://avatars.githubusercontent.com/u/2970449?v=3","gravatar_id":"","url":"https://api.github.com/users/jaumard","html_url":"https://github.com/jaumard","followers_url":"https://api.github.com/users/jaumard/followers","following_url":"https://api.github.com/users/jaumard/following{/other_user}","gists_url":"https://api.github.com/users/jaumard/gists{/gist_id}","starred_url":"https://api.github.com/users/jaumard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jaumard/subscriptions","organizations_url":"https://api.github.com/users/jaumard/orgs","repos_url":"https://api.github.com/users/jaumard/repos","events_url":"https://api.github.com/users/jaumard/events{/privacy}","received_events_url":"https://api.github.com/users/jaumard/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2016-06-30T09:06:20Z","updated_at":"2016-07-05T07:48:45Z","closed_at":"2016-06-30T09:28:43Z","body":"Hi,\r\n\r\nI'm trying to create three models Role, Resource and Permission and I want a primary key with RoleId, ResourceId and action for my Permission model. I can't manage to do it and doesn't find any info on this on the doc, it's possible ?\r\n\r\nHere is my models : \r\nRole : \r\n```\r\nmodule.exports = class Role extends Model {\r\n  static config(app, Sequelize) {\r\n    return {\r\n      options: {\r\n        classMethods: {\r\n          associate: (models) => {\r\n            models.Role.hasMany(models.Permission, {\r\n              as: 'permissions',\r\n              onDelete: 'CASCADE',\r\n              foreignKey: {\r\n                allowNull: false\r\n              }\r\n            })\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  static schema(app, Sequelize) {\r\n    return {\r\n      name: {\r\n        type: Sequelize.STRING,\r\n        allowNull: false,\r\n        primaryKey: true\r\n      },\r\n      active: {\r\n        type: Sequelize.BOOLEAN,\r\n        allowNull: false,\r\n        defaultValue: true\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\nResource: \r\n```\r\nmodule.exports = class Resource extends Model {\r\n  static config(app, Sequelize) {\r\n    return {\r\n      //More informations about supported models options here : http://docs.sequelizejs.com/en/latest/docs/models-definition/#configuration\r\n      options: {\r\n        classMethods: {\r\n          associate: (models) => {\r\n            models.Resource.hasMany(models.Permission, {\r\n              as: 'permissions',\r\n              onDelete: 'CASCADE',\r\n              foreignKey: {\r\n                allowNull: false\r\n              }\r\n            })\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  static schema(app, Sequelize) {\r\n    return {\r\n      type: {\r\n        type: Sequelize.ENUM('model', 'controller', 'other'),\r\n        allowNull: false\r\n      },\r\n      action: {\r\n        type: Sequelize.STRING,\r\n        allowNull: false\r\n      },\r\n      name: {\r\n        type: Sequelize.STRING,\r\n        allowNull: false,\r\n        primaryKey: true\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\nAnd Permission : \r\n```\r\n\r\nmodule.exports = class Permission extends Model {\r\n  static config(app, Sequelize) {\r\n    return {\r\n      options: {\r\n        classMethods: {\r\n          associate: (models) => {\r\n            models.Permission.belongsTo(models.Role, {\r\n              onDelete: 'CASCADE',\r\n              primaryKey: true,\r\n              foreignKey: {\r\n                allowNull: false\r\n              }\r\n            })\r\n            models.Permission.belongsTo(models.Resource, {\r\n              onDelete: 'CASCADE',\r\n              primaryKey: true,\r\n              foreignKey: {\r\n                allowNull: false\r\n              }\r\n            })\r\n          }\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n  static schema(app, Sequelize) {\r\n    return {\r\n      type: {\r\n        type: Sequelize.ENUM('model', 'controller', 'other'),\r\n        allowNull: false\r\n      },\r\n      action: {\r\n        type: Sequelize.ARRAY(Sequelize.STRING),\r\n        primaryKey: true,\r\n        allowNull: false\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nAt the and my primary key look like `ALTER TABLE public.permission\r\n  ADD CONSTRAINT permission_pkey PRIMARY KEY(action);` instead of `ALTER TABLE public.permission\r\n  ADD CONSTRAINT permission_pkey PRIMARY KEY(RoleId, ResourceId, action);`\r\n\r\nAny idea ?"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/230409468","html_url":"https://github.com/sequelize/sequelize/issues/6188#issuecomment-230409468","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/6188","id":230409468,"user":{"login":"janmeier","id":1210224,"avatar_url":"https://avatars.githubusercontent.com/u/1210224?v=3","gravatar_id":"","url":"https://api.github.com/users/janmeier","html_url":"https://github.com/janmeier","followers_url":"https://api.github.com/users/janmeier/followers","following_url":"https://api.github.com/users/janmeier/following{/other_user}","gists_url":"https://api.github.com/users/janmeier/gists{/gist_id}","starred_url":"https://api.github.com/users/janmeier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/janmeier/subscriptions","organizations_url":"https://api.github.com/users/janmeier/orgs","repos_url":"https://api.github.com/users/janmeier/repos","events_url":"https://api.github.com/users/janmeier/events{/privacy}","received_events_url":"https://api.github.com/users/janmeier/received_events","type":"User","site_admin":false},"created_at":"2016-07-05T07:48:45Z","updated_at":"2016-07-05T07:48:45Z","body":"You need to provide the alias `as` part to the include as well. You can also do `Item.Owners = Item.belongsToMany...` `include: [{ association: Item.Owners }]`"}}
{"actor":{"display_login":"janmeier"}}