SequelizeDatabaseError:类型“public.enum_...”不存在

lia*_*iam 7 enums backend sequelize.js typescript sequelize-typescript

我正在尝试运行一些数据库迁移,但我不断收到以下错误:

DatabaseError [SequelizeDatabaseError]: type "public.enum_companies_accountingSwStatus" does not exist

我在另一个项目中也做了同样的事情,没有出现这个问题。

这真的很困扰我,因为我在同一个表中使用 ENUMS 还有其他列,并且它们似乎工作正常。所以我不确定问题出在哪里。

这是我的文件:

公司.model.ts

const {
  employeeSize,
  revenueClass,
  accountingSwStatus,
  accountingSwType,
  paymentFreq,
  freeTrial,
} = enums.Company;

class Company extends Model {
  public id!: number;

  public employeeSize!: Enumerator;

  public revenueClass!: Enumerator;

  public accountingSwStatus!: Enumerator;

  public accountingSwType!: Enumerator;

  public static initModel(sequelize: Sequelize): void {
    Company.init(
      {
        id: {
          type: DataTypes.INTEGER,
          autoIncrement: true,
          primaryKey: true,
        },
        // works fine
        employeeSize: {
          type: DataTypes.ENUM,
          values: getValues(employeeSize),
        },
        // works fine
        revenueClass: {
          type: DataTypes.ENUM,
          values: getValues(revenueClass),
        },
        // error
        accountingSwStatus: {
          type: DataTypes.ENUM,
          values: getValues(accountingSwStatus),
        },
        accountingSwType: {
          type: DataTypes.ENUM,
          values: getValues(accountingSwType),
        },
      },
      {
        sequelize,
        modelName: 'companies',
      }
    );
  }
Run Code Online (Sandbox Code Playgroud)

迁移文件

module.exports = {
  up: async (queryInterface, Sequelize) => {
    try {
      await queryInterface.createTable('companies', {
        id: {
          type: Sequelize.INTEGER,
          autoIncrement: true,
          primaryKey: true,
        },

        // works fine
        employeeSize: {
          type: Sequelize.ENUM,
          values: ['500', '100 - 500', '50 - 100', '10 - 50', '< 10'],
        },
        // works fine
        revenueClass: {
          type: Sequelize.ENUM,
          values: [
            '> 500M',
            '100M - 500M',
            '50M - 100M',
            '10M - 50M',
            '5M - 10M',
            '1M - 5M',
            '< 1M',
          ],
        },
        // error
        accountingSwStatus: {
          type: Sequelize.ENUM,
          values: ['new', 'ended', 'failed to link'],
        },
        accountingSwType: {
          type: Sequelize.ENUM,
          values: ['quickbooks', 'xero'],
        },
      });
    } catch (err) {
      console.log(err);
    }
  },
  down: (queryInterface) => queryInterface.dropTable('companies'),
};
Run Code Online (Sandbox Code Playgroud)

我的枚举

const Company = {
  employeeSize: {
    TIER_ONE: '500',
    TIER_TWO: '100 - 500',
    TIER_THREE: '50 - 100',
    TIER_FOUR: '10 - 50',
    TIER_FIVE: '< 10',
  },
  revenueClass: {
    TIER_ONE: '> 500M',
    TIER_TWO: '100M - 500M',
    TIER_THREE: '50M - 100M',
    TIER_FOUR: '10M - 50M',
    TIER_FIVE: '5M - 10M',
    TIER_SIX: '1M - 5M',
    TIER_SEVEN: '< 1M',
  },
  accountingSwStatus: {
    NEW: 'new',
    EXISTING: 'existing',
    FAIL_TO_LINK: 'failed to link',
  },
  accountingSwType: {
    QUICKBOOKS: 'quickbooks',
    XERO: 'xero',
  },
  paymentFreq: {
    MONTHLY: 'monthly',
    YEARLY: 'yearly',
  },
  freeTrial: {
    YES: 'yes',
    NO: 'no',
    ENDED: 'ended',
  },
};

export default {
  Company,
};
Run Code Online (Sandbox Code Playgroud)

小智 0

我发现对自己有用的一个解决方案是,使用 Sequelize.DataTypes.ENUM 代替 Sequelize.ENUM ,似乎在迁移中 Sequelize.ENUM 存在问题,因为我发现的大多数答案只是使用纯 sql 完成的