title: Sequelize Composite Foreign Key
body: <p>I have a database with the following tables:</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `app_user` (
    `user_id` INT NOT NULL,
    `user_name` VARCHAR(45) NOT NULL,
    PRIMARY KEY (`user_id`))
  ENGINE = InnoDB;
  
  CREATE TABLE IF NOT EXISTS `user_folder` (
    `user_id` INT NOT NULL,
    `document_id` INT NOT NULL,
    PRIMARY KEY (`user_id`, `document_id`),
    CONSTRAINT `fk_user_document_user`
      FOREIGN KEY (`user_id`)
      REFERENCES `zinc`.`app_user` (`user_id`)
      ON DELETE CASCADE
      ON UPDATE CASCADE)
  ENGINE = InnoDB;
  
  CREATE TABLE IF NOT EXISTS `folder_content` (
    `user_id` INT NOT NULL,
    `document_id` INT NOT NULL,
    `content_id` INT NOT NULL,
    PRIMARY KEY (`user_id`, `document_id`, `content_id`),
    CONSTRAINT `fk_folder_content_folder`
      FOREIGN KEY (`user_id` , `document_id`)
      REFERENCES `zinc`.`user_folder` (`user_id` , `document_id`)
      ON DELETE CASCADE
      ON UPDATE CASCADE)
  ENGINE = InnoDB;
  </code></pre>
  
  <p>I need to create a Sequelize model to represent it. The only problem I have is with the relation folder_content and user_folder because of the composite key. </p>
  
  <p>How can I create this sequelize model? </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>var AppUser = sequelize.define('app_user', 
  {userId: {type: Sequelize.INTEGER, primaryKey: true, field: 'user_id'}, ... } );
  
  var UserFolder = sequelize.define('user_folder', 
  {userId: {type: Sequelize.INTEGER, primaryKey: true, field: 'user_id'}, 
  documentId: {type: Sequelize.INTEGER, primaryKey: true, field: 'document_id'}... });
  
  var FolderContent = sequelize.define('folder_content', {
  userId: {type: Sequelize.INTEGER, primaryKey: true, field: 'user_id'}, 
  documentId: {type: Sequelize.INTEGER, primaryKey: true, field: 'document_id'}, 
  contentId: {type: Sequelize.INTEGER, primaryKey: true, field: 'content_id'}... });
  
  
  UserFolder.hasMany(FolderContent);
  FolderContent.belongsTo(UserFolder, {foreingKey: !! });// &lt;- PROBLEM
  </code></pre>