如何在sequelize中定义多列的唯一索引

时间:2020-12-27 04:28:38

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.

如何在sequelize中的列组合上定义唯一索引。例如,我想在user_id,count和name上添加唯一索引。

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,
        })

3 个解决方案

#1


16  

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

你可以参考这个文档http://docs.sequelizejs.com/en/latest/docs/models-definition/#indexes

You will need to change your defination 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']
        }
    ]
})

#2


3  

I have same issue to applied composite unique constraint to multiple columns but nothing work with Mysql, Sequelize(4.10.2) and NodeJs 8.9.4 finally I fixed through following code.

我对将多个列应用复合唯一约束有同样的问题,但是没有使用Mysql,Sequelize(4.10.2)和NodeJs 8.9.4,最后我修复了下面的代码。

queryInterface.createTable('actions', {
  id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
  },
  system_id: {
      type: Sequelize.STRING,
      unique: 'actions_unique',
  },
  rule_id: {
      type: Sequelize.STRING,
      unique: 'actions_unique',
  },
  plan_id: {
      type: Sequelize.INTEGER,
      unique: 'actions_unique',
  }
}, {
  uniqueKeys: {
      actions_unique: {
          fields: ['system_id', 'rule_id', 'plan_id']
      }
  }
});

#3


2  

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',
        })

#1


16  

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

你可以参考这个文档http://docs.sequelizejs.com/en/latest/docs/models-definition/#indexes

You will need to change your defination 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']
        }
    ]
})

#2


3  

I have same issue to applied composite unique constraint to multiple columns but nothing work with Mysql, Sequelize(4.10.2) and NodeJs 8.9.4 finally I fixed through following code.

我对将多个列应用复合唯一约束有同样的问题,但是没有使用Mysql,Sequelize(4.10.2)和NodeJs 8.9.4,最后我修复了下面的代码。

queryInterface.createTable('actions', {
  id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
  },
  system_id: {
      type: Sequelize.STRING,
      unique: 'actions_unique',
  },
  rule_id: {
      type: Sequelize.STRING,
      unique: 'actions_unique',
  },
  plan_id: {
      type: Sequelize.INTEGER,
      unique: 'actions_unique',
  }
}, {
  uniqueKeys: {
      actions_unique: {
          fields: ['system_id', 'rule_id', 'plan_id']
      }
  }
});

#3


2  

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',
        })