I'm trying to create a unique constraint on multiple columns in my postgres DB using sequelize. The instructions here are pretty clear but they don't work. Here's what my migration file looks like:
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('scores', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
opp: {
type: Sequelize.INTEGER,
allowNull: false,
unique: 'compositeScoreIndex'
},
oppt: {
type: Sequelize.STRING,
allowNull: false,
unique: 'compositeScoreIndex'
},
tree: {
type: Sequelize.STRING,
allowNull: false,
unique: 'compositeScoreIndex'
},
version: {
type: Sequelize.INTEGER,
allowNull: false,
unique: 'compositeScoreIndex'
},
score: {
type: Sequelize.JSON,
allowNull: false
}
}, {
indexes: [{
unique: true,
fields: ['opp', 'oppt', 'tree', 'version']
}]
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('scores');
}
};
This should be equivalent to this query:
CREATE TABLE scores (id INT PRIMARY KEY NOT NULL, opp INT NOT NULL,
oppt TEXT NOT NULL, tree TEXT NOT NULL, version INT NOT NULL, score
JSON NOT NULL, CONSTRAINT uniq_scores UNIQUE(opp, oppt, tree, version));
But the sequelize migration fails to create the constraint. I'm able to create multiple rows with the same values in opp, oppt, tree and version. Anyone else seeing this?
UPDATE: I was able to have the migration print out log statements of the SQL queries. Here's the CREATE TABLE query it runs with the above migration file:
CREATE TABLE IF NOT EXISTS "scores" ("id" SERIAL , "oppId" INTEGER NOT NULL,
"oppType" VARCHAR(255) NOT NULL, "tree" VARCHAR(255) NOT NULL,
"version" INTEGER NOT NULL, "score" JSON NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Clearly there is no mention of a unique constraint. Anyone know why Sequelize isn't behaving as expected here?