How to define unique index on multiple columns in sequelize

前端 未结 6 2113
半阙折子戏
半阙折子戏 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 06:59

    You can refer to this doc http://docs.sequelizejs.com/en/latest/docs/models-definition/#indexes

    You will need to change your definition like shown below and call sync

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

提交回复
热议问题