簡體   English   中英

無法跨一對多查詢 NestJS 中的屬性 Postgresql

[英]Cannot query across one-to-many for property in NestJS Postgresql

那是 updateEntity.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>

這是我要更新屬性的 function

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"}]

我收到錯誤消息“無法跨一對多查詢屬性位置”我只想能夠更新公司

沒關系,因為如果您需要從 Location 實體更新、創建或刪除數據,則需要使用該實體而不是來自已加入實體的實體進行查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM