查询时,Sequelize 模型指向错误的模型

问题描述

我有两个模型,CustomerSignupStatus,但是似乎如果我查询 CustomerModel.findAll(),生成查询正在查询 SignupStatus 表。

因此,我无法正确查询任何内容

另一件事是我不断收到错误 TypeError: Cannot read property 'Sequelize' of undefined。我找到了一些仅表明关联有问题的答案。但是,我似乎在我的代码中找不到任何导致模型被错误引用的错误...

有人有什么想法吗?

编辑:

看来,虽然我的导入路径是 import CustomerModel from '../db/models/customers.model';,但实际导入的 Model 是 SignupStatusModel。

这很奇怪,因为 SignupStatus 的路径是 '../db/models/signupStatus.model'

这是我的文件

Customer.model.ts

class Customer extends Model {
  public id!: number;

  public prefix: string;

  public firstName: string;

  public lastName: string;

  public email: string;

  public readonly createdAt!: Date;

  public readonly updatedAt!: Date;

  public static initModel(sequelize: Sequelize): void {
    Customer.init(
      {
        id: {
          type: DataTypes.INTEGER,autoIncrement: true,primaryKey: true,},firstName: {
          type: new DataTypes.STRING(255),lastName: {
          type: new DataTypes.STRING(255),email: {
          type: new DataTypes.STRING(255),allowNull: false,{
        sequelize,modelName: 'customers',}
    );
  }

  public static initAssociation(): void {
    this.hasOne(SignupStatus);
  }
}

SignupStatus.model.ts

class SignupStatus extends Model {
  public id!: number;

  public customerId: number;

  public companyId: number;

  public emailVerified: boolean;

  public emailVerificationCompletionDate: Date;

  public customerProfileCompleted: string;

  public customerProfileCompletionDate: Date;

  public readonly createdAt!: Date;

  public readonly updatedAt!: Date;

  public static initModel(sequelize: Sequelize): void {
    Customer.init(
      {
        id: {
          type: DataTypes.INTEGER,customerId: {
          type: DataTypes.INTEGER,companyId: {
          type: DataTypes.INTEGER,emailVerified: {
          type: DataTypes.BOOLEAN,defaultValue: false,emailVerificationCompletionDate: {
          type: DataTypes.DATE,customerProfileCompleted: {
          type: DataTypes.BOOLEAN,customerProfileCompletionDate: {
          type: DataTypes.DATE,modelName: 'signupStatus',}
    );
  }

  public static initAssociation(): void {
    this.belongsTo(Customer);
  }
}

index.model.ts

export const sequelize = new Sequelize(
  process.env.POSTGRES_DATABASE,process.env.POSTGRES_USERNAME,process.env.POSTGRES_PASSWORD,{
    host: process.env.POSTGRES_HOST,dialect: 'postgres',logging: true,pool: {
      min: 0,max: 30,idle: 10000,acquire: 30000,dialectOptions: {
      // same as host string above
      socketPath: process.env.POSTGRES_HOST,}
);

export const initModels = async (sequelizeInst: Sequelize) => {
  console.info('Initializing sequelize models...');
  await Customer.initModel(sequelizeInst);
  await SignUpStatus.initModel(sequelizeInst);
};

export const initAssociation = async () => {
  console.info('Initializing sequelize associations...');
  await Customer.initAssociation();
  await SignUpStatus.initAssociation();
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)