Sequelize composite unique constraint
Defining model:
export default function(sequelize, DataTypes) {
return sequelize.define('Item', {
minor: DataTypes.INTEGER,
major: DataTypes.INTEGER,
});
}
Can I define the pair minor and major as a composite UNIQUE constraint?
Recently, in V4, Sequelize has a method addConstraint
on the query interface:
queryInterface.addConstraint('Items', {
fields: ['minor', 'major'],
type: 'unique',
name: 'custom_unique_constraint_name'
});
queryInterface.createTable('Item', {
minor: {
type: Sequelize.INTEGER,
},
major: {
type: Sequelize.INTEGER,
}
}, {
uniqueKeys: {
Items_unique: {
fields: ['minor', 'major']
}
}
});