Unique constraint across foreign keys in Sequelize model

you can use the unique constraint and give it a string rather than a bool. Then another other fields with the same string will become part of the same composite index.

i.e.:

module.exports = function (sequelize, DataTypes) {
   var Votes = sequelize.define('Votes', {
      isUpVote: {
          type: DataTypes.BOOLEAN,
          unique: 'myCompositeIndexName'
      },
      TrackId: {
          type: DataType.INTEGER
          unique: 'myCompositeIndexName',
      },
      UserId: {
          type: DataType.INTEGER
          unique: 'myCompositeIndexName',
      }
   }, {
       classMethods: {
           associate: function (models) {
               Votes.belongsTo(models.Track);
               Votes.belongsTo(models.User);
           }
       }
    });

    return Votes;
}

(^Not tested, just off the top of my head!)

The problem with this is that this will only occur during the creation of a table. If you table already exists, you can achieve this by using the migrations feature of sequelize-cli.

I really hope this helps else at least points you in the right direction. If you are still stuck, I recommend you go to the IRC channel for sequelize as it seems to be pretty active.


What ended up working for me was adding it under indexes, e.g.:

module.exports = function (sequelize, DataTypes) {
  var Votes = sequelize.define('Votes', {
    isUpVote: DataTypes.BOOLEAN,
  }, {
    indexes: [
      {
        unique: true,
        fields: ['TrackId', 'UserId'],
      },
    ],
    classMethods: {
      associate: function (models) {
        Votes.belongsTo(models.Track);
        Votes.belongsTo(models.User);
      },
    },
  });

  return Votes;
}

Tags:

Sequelize.Js