这就是 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>
这是我要更新属性的函数
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"}]
我收到错误“无法一对多查询房产位置” 我只是希望能够更新公司信息
没关系,因为如果您需要更新、创建或删除位置实体中的数据,您需要使用该实体而不是连接实体进行查询
你找到答案了吗?