Sequelize Many to Many 关联错误 - AggregateError

Error on Sequelize Many to Many association - AggregateError

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

@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[];

}

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

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 at recursiveBulkCreate (/home/jose/Documents/prop-api/node_modules/sequelize/lib/model.js:2600:17) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async Function.bulkCreate (/home/jose/Documents/prop-api/node_modules/sequelize/lib/model.js:2824:12) at async Promise.all (index 0) at async BelongsToMany.add (/home/jose/Documents/prop-api/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30) (Use node --trace-warnings ... to show where the warning was created) <node_internals>/internal/process/warning.js:42 (node:9252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) <node_internals>/internal/process/warning.js:42 (node:9252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

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

我解决了问题,好像是这一行的问题

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

我删除了整个过程中的时间戳 table 并且还更改了保存业务的代码以传递文字对象而不是模型对象。

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