javascript 删除 sequelize 迁移中的约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29518786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:39:47  来源:igfitidea点击:

Remove constraints in sequelize migration

javascriptsqlconstraintssequelize.js

提问by soerface

I'm adding a uniqueconstraint in a migration via the migrations.changeColumnfunction.

我正在unique通过migrations.changeColumn函数在迁移中添加约束。

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.

添加约束有效,但由于您需要提供“向后迁移”,因此以相同的方式删除它不会。向后迁移时它不会给出任何错误,但再次应用前向迁移结果在Possibly unhandled SequelizeDatabaseError: relation "myAttribute_unique_idx" already exists.

(The used database is postgres)

(使用的数据库是postgres)

module.exports = {
  up: function (migration, DataTypes, done) {
    migration.changeColumn(
      'Users',
      'myAttribute',
      {
        type: DataTypes.STRING,
        unique: true                 // ADDING constraint works
      }
    ).done(done);
  },

  down: function (migration, DataTypes, done) {
    migration.changeColumn(
      'Users',
      'myAttribute',
      {
        type: DataTypes.STRING,
        unique: false                // REMOVING does not
      }
    ).done(done);
  }
};

I also tried using removeIndex

我也尝试使用removeIndex

migration.removeIndex('Users', 'myAttribute_unique_idx').done(done);

But it gives the following error when reverting the migration:

但是在恢复迁移时会出现以下错误:

Possibly unhandled SequelizeDatabaseError: cannot drop index "myAttribute_unique_idx" because constraint myAttribute_unique_idx on table "Users" requires it

回答by Tim Givois

As of 2017 with Sequelize 4.4.2, we can remove constraints with queryInterface API:

从 2017 年的 Sequelize 4.4.2 开始,我们可以使用 queryInterface API 移除约束:

queryInterface.removeConstraint(tableName, constraintName)

queryInterface.removeConstraint(tableName, constraintName)

Documentation is here.

文档在这里

回答by ezpn

Unfortunately sequelize doesn't have a builtin migration method to remove constraint. That is why before removing key you need to make a raw query.

不幸的是,sequelize 没有内置的迁移方法来移除约束。这就是为什么在删除键之前需要进行原始查询的原因。

down: function (migration, DataTypes) {
  migration.sequelize.query(
    'ALTER TABLE Users DROP CONSTRAINT myAttribute_unique_idx;'
  );
  migration.removeIndex('Users', 'myAttribute_unique_idx');

  return;
}

回答by Keshav Aggarwal

Sequelize has removeConstraint()method if you want to remove the constraint. So you can have use something like this:

removeConstraint()如果要删除约束,Sequelize 有方法。所以你可以使用这样的东西:

return queryInterface.removeConstraint('users', 'users_userId_key', {})

return queryInterface.removeConstraint('users', 'users_userId_key', {})

where usersis my Table Name and users_userId_keyis index or constraint name which is generally of the form attributename_unique_keyif you have unique constraint which you wanna remove(say).

哪里users是我的表名,users_userId_key是索引或约束名称,attributename_unique_key如果您有想要删除的唯一约束(比如说),通常采用这种形式。

回答by bragi

If you want to remove the index you should use:

如果要删除索引,则应使用:

down: function (migration, DataTypes) {
    return migration.removeIndex('Users', 'myAttribute_unique_idx');
}

The return is used to use promise style instead of callbacks. This is recommended by sequelize.

return 用于使用 promise 样式而不是回调。这是 sequelize 推荐的。

It would also be good to handle the creation of the index on your own as descripted here: http://sequelize.readthedocs.org/en/latest/docs/migrations/#addindextablename-attributes-options

按照此处的描述自行处理索引的创建也很好:http: //sequelize.readthedocs.org/en/latest/docs/migrations/#addindextablename-attributes-options