【问题标题】:Sequelize migration drop column before table was created在创建表之前对迁移删除列进行 Sequelize
【发布时间】:2021-08-03 16:39:34
【问题描述】:

我在我的项目中使用 Sequelize。在第三次迁移中,我创建了表 Comments,其中包含名为 type 的字段。经过一些迁移后,我有一个删除该字段的迁移。但是,当我做migrate:allsequelize 尝试在创建表之前删除列:

await queryInterface.removeColumn('Comments', 'type');

有时没有发生错误。但我经常得到:

ERROR: Can't DROP 'type'; check that column/key exists

如何解决这个问题?

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    我通过在迁移开始之前在粉碎迁移文件中添加“睡眠”来解决它:

    module.exports = {
      up: async (queryInterface, Sequelize) => {
        await sleep();
        await queryInterface.removeColumn('Comments', 'type');
      },
    
      down: async (queryInterface, Sequelize) => {
        await queryInterface.addColumn('Comments', 'type', {
          type: Sequelize.ENUM,
          allowNull: false,
          values: commentTypes,
          defaultValue: 'comment',
        });
      },
    };
    
    function sleep() {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve();
        }, 5000);
      });
    }
    

    我知道这不是一个好的解决方案,但它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-03
      • 2016-11-15
      • 2019-01-09
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      相关资源
      最近更新 更多