How to define unique index on multiple columns in sequelize

前端 未结 6 2121
半阙折子戏
半阙折子戏 2020-12-13 06:50

How do I define a unique index on a combination of columns in sequelize. For example I want to add a unique index on user_id, count and name.

var Tag = sequ         


        
6条回答
  •  北海茫月
    2020-12-13 07:03

    If the accepted one is not working then try the below code. It worked for me in my case rather the accepted one.

    var Tag = sequelize.define('Tag', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        user_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            unique: 'uniqueTag',
        },
        count: {
            type: DataTypes.INTEGER(11),
            allowNull: true,
            unique: 'uniqueTag',
        },
        name: {
            type: DataTypes.STRING,
            allowNull: true,
            unique: 'uniqueTag',
        }
    });
    

提交回复
热议问题