4

我创建了一个迁移文件以将列添加为up,然后在down.

这是迁移文件代码:

module.exports = {
  up: (queryInterface, Sequelize) =>
    queryInterface.addColumn('Books', 'Rating', {
      allowNull: false,
      type: Sequelize.ENUM('like', 'dislike'),
    }),

  down: (queryInterface, Sequelize) => {
    queryInterface.removeColumn('Books', 'Rating');
  },
};

当我第一次使用 运行它时db:migrate,它成功添加了列,但是当我执行 adb:migrate:undo:all然后再次运行迁移时,它给我一个错误 sqing

======= 20180211100937-AddedRatingIntoBooks: migrating 
======= 2018-02-11 15:42:46.076 IST 
[64531] ERROR:  type "enum_Books_Rating" already exists 2018-02-11 15:42:46.076 IST 
[64531] STATEMENT:  CREATE TYPE "public"."enum_Books_Rating" AS ENUM('like', 'dislike');
ALTER TABLE "public"."Boo ks" ADD COLUMN "Rating" "public"."enum_Books_Rating";

    ERROR: type "enum_Books_Rating" already exists

这里的问题仍然存在。

4

1 回答 1

9

Sequelize 为您定义的每个枚举创建类型,您可以在此处找到

ENUM 类型的名称是“enum”、表名和蛇形外壳中的列名的串联。(enum_Books_Rating这里)

要为 创建迁移ENUM,您必须像这样修改 down 函数:

module.exports = {
  up: (queryInterface, Sequelize) =>
    queryInterface.addColumn('Books', 'Rating', {
      allowNull: false,
      type: Sequelize.ENUM('like', 'dislike')
  }),

  down: (queryInterface, Sequelize) =>  
    queryInterface.removeColumn('Books', 'Rating')
      .then(() => queryInterface.sequelize.query('DROP TYPE "enum_Books_Rating";'));
  };

希望这可以帮助。

于 2018-02-11T11:24:46.953 回答