nestjs pgsql typeorm ON CONFLICT DO UPDATE 命令无法再次影响行

问题描述 投票:0回答:1

我有这个实体

@Entity(DB_TABLES.PAYMENTS.NAME)
@Unique(["paidAmount", "paidDate", "centrelinkRef"])
export class PaymentEntity extends CommonEntity {

  @Column({ name: "actual_amount", type: "decimal", precision: 10, scale: 2, nullable: true })
  actualAmount: number;

  @Column({ name: "payment_status", type: "enum", enum: PaymentStatus, default: PaymentStatus.Paid })
  paymentStatus: PaymentStatus;

  @Column({ name: "paid_amount", type: "decimal", precision: 10, scale: 2, nullable: true, default: 0 })
  paidAmount: number;

  @Column({ name: "paid_date", type: "timestamptz", nullable: true })
  paidDate: Date;

  @Column({ name: "centrelink_ref", type: "varchar", nullable: true })
  centrelinkRef: string;

}

并添加付款

 async _addValidPayments(queryRunnerManager: EntityManager, payments: PaymentDto[]): Promise<ApiResponseDto> {
    // Save the updated entities
    const conflictPaths = ["paidAmount", "paidDate", "centrelinkRef"];
    const res = await queryRunnerManager.upsert(PaymentEntity, payments, {
      conflictPaths, skipUpdateIfNoValuesChanged: true, upsertType: "on-conflict-do-update"
    });
    console.log("PaymentService ~ _addValidPayments ~ res:", res);
    return { msg: APP_MESSAGE.CREATED_SUCCESSFULLY };
  }

但这显示错误

[Nest] 4320  - 02/19/2024, 3:04:06 PM   ERROR [ExceptionsHandler] 
ON CONFLICT DO UPDATE command cannot affect row a second time     
QueryFailedError: ON CONFLICT DO UPDATE command cannot affect row 
a second time
    at PostgresQueryRunner.query (E:\PROJECTS\Neha\orange_rentals\backend\src\driver\postgres\PostgresQueryRunner.ts:299:19)        
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

有什么问题吗?

database postgresql nestjs typeorm
1个回答
0
投票
 const sqlQuery = `
      INSERT INTO payments ("title",.....)
      VALUES 
      ${payments.map((data) => `('${data.title}'`).join(',')}
      ON CONFLICT ("paid_amount", "paid_date", "payment_source", "title")
      DO NOTHING
      RETURNING id`;
res = await queryRunnerManager.query(sqlQuery);

这对我有用

© www.soinside.com 2019 - 2024. All rights reserved.