【问题标题】:Error on Sequelize Many to Many association - AggregateErrorSequelize 多对多关联时出错 - AggregateError
【发布时间】:2021-07-09 07:30:01
【问题描述】:

我的应用中有以下模型设计:

@Table({ freezeTableName: true, tableName: 'Business', timestamps: true})
export class Business extends Model<Business> {

  @Unique(true)
  @Column(DataType.STRING(255))
  businessName: string;

  @Default(BusinessStatusEnum.pending_verify)
  @AllowNull(false)
  @Column(DataType.ENUM(BusinessStatusEnum.active, BusinessStatusEnum.canceled, BusinessStatusEnum.pending_payment, BusinessStatusEnum.pending_verify))
  businessStatus: string;

  @AllowNull(true)
  @Column(DataType.STRING)
  businessVerificationMessageId: string;

  @AllowNull(false)
  @Default(false)
  @Column(DataType.BOOLEAN)
  businessVerifiedEmail: boolean;

  // Many to Many association

  @BelongsToMany(() => SubscriptionPlan, () => BusinessSubscriptionPlan)
  subscriptionPlans: SubscriptionPlan[];

  @HasMany(() => BusinessSubscriptionPlan)
  businessSubscriptionPlans: BusinessSubscriptionPlan[];

}


@Table({ freezeTableName: true, tableName: 'SubscriptionPlan', timestamps: true})
export class SubscriptionPlan extends Model<SubscriptionPlan> {

  @AllowNull(false)
  @Unique(true)
  @Column(DataType.STRING(255))
  subscriptionPlanName: string;

  // Many to Many association

  @BelongsToMany(() => Business, () => BusinessSubscriptionPlan)
  businesses: Business[];

  @HasMany(() => BusinessSubscriptionPlan)
  businessSubscriptionPlans: BusinessSubscriptionPlan[];

}

@Table({ freezeTableName: true, tableName: 'BusinessSubscriptionPlan', timestamps: true})
export class BusinessSubscriptionPlan extends Model<BusinessSubscriptionPlan> {

  @PrimaryKey
  @AllowNull(false)
  @AutoIncrement
  @Column(DataType.INTEGER)
  id: number;

  @AllowNull(false)
  @Column(DataType.ENUM(BusinessSubscriptionPeriodEnum.monthly, BusinessSubscriptionPeriodEnum.annual))
  businessSubscriptionPeriod: string;

  @AllowNull(false)
  @Column(DataType.ENUM(BusinessSubscriptionPlanStatusEnum.active, BusinessSubscriptionPlanStatusEnum.upgraded, BusinessSubscriptionPlanStatusEnum.canceled))
  businessSubscriptionStatus: string;

  @ForeignKey(() => Business)
  @Column(DataType.INTEGER)
  businessId: number

  @BelongsTo(() => Business, 'businessId')
  business: Business;

  @ForeignKey(() => SubscriptionPlan)
  @Column(DataType.INTEGER)
  subscriptionPlanId: number

  @BelongsTo(() => SubscriptionPlan, 'subscriptionPlanId')
  subscriptionPlan: SubscriptionPlan;

  @HasMany(() => PaymentReceipt )
  paymentReceipts: PaymentReceipt[];

}

在我的代码中,我试图保存一个业务对象以及 Business 和 SubscriptionPlan 之间的关系表。

try {
                    business.isNewRecord = true;
                    const savedBusiness = await business.save();
                    await savedBusiness.reload();
                    if (subscriptionPlan && businessSubscriptionPlan) {
                        savedBusiness.addSubscriptionPlan(subscriptionPlan, { through: businessSubscriptionPlan });
                    }
                    return savedBusiness;
                } catch (error) {
                    throw new CustomError(messages[2].message.replace("${object}", "Business"), 500, error);
            }

我收到了这个错误

(node:9252) UnhandledPromiseRejectionWarning: AggregateError 在 recursiveBulkCreate (/home/jose/Documents/prop-api/node_modules/sequelize/lib/model.js:2600:17) 在 processTicksAndRejections (internal/process/task_queues.js:93:5) 在异步 Function.bulkCreate (/home/jose/Documents/prop-api/node_modules/sequelize/lib/model.js:2824:12) 在异步 Promise.all(索引 0) 在异步 BelongsToMany.add (/home/jose/Documents/prop-api/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30) (使用node --trace-warnings ... 显示警告的创建位置) /internal/process/warning.js:42 (节点:9252)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:2) /internal/process/warning.js:42 (节点:9252)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

我不知道我的模型是否有问题,或者是在保存业务的实际代码中,在模型中我遵循了这个文档https://sequelize.org/master/manual/advanced-many-to-many.html 如果您能给我任何帮助,我将不胜感激。

【问题讨论】:

    标签: node.js typescript sequelize.js sequelize-typescript


    【解决方案1】:

    我解决了这个问题,看来问题出在这一行

    @Table({ freezeTableName: true, tableName: 'BusinessSubscriptionPlan', timestamps: true})
    

    我从直通表中删除了时间戳,还更改了保存业务的代码以传递文字对象而不是模型对象。

    business.isNewRecord = true;
                        const savedBusiness = await business.save();
                        await savedBusiness.reload();
                        if (subscriptionPlan && businessSubscriptionPlan) {
                            savedBusiness.addSubscriptionPlan(subscriptionPlan, { through: {
                                businessSubscriptionPeriod: businessSubscriptionPlan.businessSubscriptionPeriod,
                                businessSubscriptionStatus: businessSubscriptionPlan.businessSubscriptionStatus
                            } });
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 2014-04-15
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多