【问题标题】:Error: Cannot query across many-to-many for property on updating错误:更新时无法跨多对多查询属性
【发布时间】:2022-03-16 04:13:47
【问题描述】:

我正在尝试更新许多可能的关系。

export class CreateProductDto {
  @ApiProperty()
  @IsString()
  description: string;

  @ApiProperty()
  @IsString()
  name: string;

  @ApiProperty({ isArray: true })
  @IsNumber({}, { each: true })
  @IsArray()
  categoryIds: number[];
}
export class UpdateProductDto extends PartialType(CreateProductDto) {}
export class ProductsService {
  constructor(
    @InjectRepository(Product)
    private productRepository: Repository<Product>,
    private categoriesService: CategoriesService,
  ) {}

  async update(id: number, updateProductDto: UpdateProductDto) {
    let categories: Category[] = undefined;
    if (updateProductDto.categoryIds) {
      categories = await Promise.all(
        updateProductDto.categoryIds.map(
          async (id) => await this.categoriesService.findOneOrFail(id),
        ),
      );
      delete updateProductDto.categoryIds;
    }
    await this.productRepository.update(
      { id },
      { ...updateProductDto, categories },
    );
    return await this.findOneOrFail(id);
  }

  async findOneOrFail(id: number) {
    const product = await this.productRepository.findOne({ id });
    if (product) {
      return product;
    }
    throw new BadRequestException(`Product is not present`);
  }
}
@Entity()
export class Product extends BaseEntity {
  @Column()
  description: string;

  @Column()
  name: string;

  @ManyToMany(() => Category, (object) => object.products, {
    cascade: true,
    eager: true,
  })
  @JoinTable()
  categories: Category[];

}
@Entity()
export class Category extends BaseEntity {
  @Column()
  name: string;

  @ManyToMany(() => Product, (object) => object.categories)
  products: Product[];
}

最后,当我尝试使用此有效负载调用 ProductsService.update

"categoryIds": [ 2 ]

我遇到了这样的错误 Error: Cannot query across many-to-many for property categories

可以帮我更新多对多

【问题讨论】:

    标签: postgresql sql-update many-to-many nestjs typeorm


    【解决方案1】:

    解决我使用的问题。

    async dummyUpdate(objetUpdateDto:ObjectUpdateDto):Promise<TypeReturn>{
    const {idObjectToUpdate,colum1ToUpate,colum2ToUpate...} = objetUpdateDto;
     try{
       const objectToUpdate = await this.repositoryOfEntity.findOne(idObjectToUpdate);
       idObjectToUpdate.colum1ToUpate = colum1ToUpate;
       idObjectToUpdate.colum2ToUpate = colum2ToUpate;
       await this.repositoryOfEntity.save(objectToUpdate);
       return objectToUpdate ,
    
    }catch(err){
        throw new ConflictException("your message"+err);
    }
    
    
    
    }
    

    【讨论】:

      【解决方案2】:

      在您的类别实体中添加 Product 的关系 id 并在更新实体时使用 save 方法而不是 update

      @Entity()
      export class Category extends BaseEntity {
        @Column()
        name: string;
      
        @ManyToMany(() => Product, (object) => object.categories)
        products: Product[];
      
        // Add this 
      
        @Column()
        productId: string;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-26
        • 2022-01-15
        • 2012-11-27
        • 2016-08-19
        • 1970-01-01
        • 1970-01-01
        • 2018-02-24
        相关资源
        最近更新 更多