首页 > 解决方案 > 唯一键没有像 postgres 中的其他字段一样更新

问题描述

我正在开发一个 NestJs 项目,一切似乎都很好,直到我尝试更新,我发现我尝试更改为另一个 UUID 的 UUID 来自不同的表并没有更新。虽然我没有收到任何错误,但很明显有些事情我做得不对。下面是代码列表:

consumablePack.repository.ts

 async updateConsumablePack(
    id: string,
    updateConsumablePackDto: UpdateConsumablePackDto
  ): Promise<ConsumablePack> {
    const { product, quantity, unit, productId } = updateConsumablePackDto;
    const consumablePack = new ConsumablePack();

    consumablePack.product = product;
    consumablePack.quantity = quantity;
    consumablePack.unit = unit;
    consumablePack['_productId'] = productId;
    console.log('====================================');
    console.log(Product);
    console.log('====================================');
    return this.save({ id, ...consumablePack });
  }

consumablePack.entity.ts

@Entity()
export class ConsumablePack {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ManyToOne(() => Product, (product) => product.id, {
    cascade: ['insert', 'update'],
  })
  @JoinColumn()
  product: Product;

  @Column({ nullable: true })
  quantity: number;

  @Column({
    type: 'enum',
    enum: UnitOptions,
    default: UnitOptions.PIECE,
  })
  unit: UnitOptions;
}

consumablePack.dto.ts

export class CreateConsumablePackDto {
  @IsUUID()
  productId: string;

  product: Product;

  @IsNotEmpty()
  quantity: number;

  @IsIn(['PIECE', 'CARTON', 'PACK'])
  unit: UnitOptions;
}

export class UpdateConsumablePackDto {
  productId: string;

  product: Product;

  quantity: number;

  @IsIn(['PIECE', 'CARTON', 'PACK'])
  unit: UnitOptions; 
}

标签: javascripttypescriptnestjstypeormrelation

解决方案


推荐阅读