I’m adding a unique
constraint in a migration via the migrations.changeColumn function.
Adding the constraint works, but since you need to provide a “backwards migration“, removing it the same way does not. It doesn’t give any errors when migrating backwards, but again applying the forward migration results in Possibly unhandled SequelizeDatabaseError: relation "myAttribute_unique_idx" already exists
.
(The used database is postgres)
JavaScript
x
24
24
1
module.exports = {
2
up: function (migration, DataTypes, done) {
3
migration.changeColumn(
4
'Users',
5
'myAttribute',
6
{
7
type: DataTypes.STRING,
8
unique: true // ADDING constraint works
9
}
10
).done(done);
11
},
12
13
down: function (migration, DataTypes, done) {
14
migration.changeColumn(
15
'Users',
16
'myAttribute',
17
{
18
type: DataTypes.STRING,
19
unique: false // REMOVING does not
20
}
21
).done(done);
22
}
23
};
24
I also tried using removeIndex
JavaScript
1
2
1
migration.removeIndex('Users', 'myAttribute_unique_idx').done(done);
2
But it gives the following error when reverting the migration:
JavaScript
1
2
1
Possibly unhandled SequelizeDatabaseError: cannot drop index "myAttribute_unique_idx" because constraint myAttribute_unique_idx on table "Users" requires it
2
Advertisement
Answer
As of 2017 with Sequelize 4.4.2, we can remove constraints with queryInterface API:
queryInterface.removeConstraint(tableName, constraintName)
Documentation is here.