NodeJS 无法对NestJS Postgresql中的属性进行一对多查询

e4yzc0pl  于 2023-01-30  发布在  Node.js
关注(0)|答案(1)|浏览(171)

这是更新实体.ts

import { IsNotEmpty } from 'class-validator'
import { BaseEntity, Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
import { Company } from './company.entity'

@Entity('countries')
export class Country extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string

  @IsNotEmpty()
  @Column({ unique: true })
  name: string

  @ManyToOne(() => Company, (company) => company.locations, { nullable: true })
  @JoinColumn({ name: 'company_id' })
  countryId: Company[]
}

带有位置字段的CompanyEntity.ts

@OneToMany(() => Country, (country) => country.countryId, { eager: true })
  locations: Array<Country>

这是我想要更新属性的函数

async update(id: number, updateCompanyDto: UpdateCompanyDto) {
    const newLocations = updateCompanyDto.locations.map((location) => Country.create(location))
    updateCompanyDto.locations = newLocations
    const status = await Company.update(id, updateCompanyDto)
    if (status.affected <= 0) {
      throw new HttpException('This company does not exist', HttpStatus.NOT_FOUND)
    }
    return status
  }

第一次使用OneToMany和ManyToMany,如果我有这样的请求体

"locations": [{"name":"Paris"}]

我收到错误“无法跨一对多查询房产位置”,我只希望能够更新公司

6jygbczu

6jygbczu1#

这是因为如果需要更新、创建或删除位置实体中的数据,则需要使用该实体而不是连接实体进行查询

相关问题