Nestjs 多通关系:更改时更新父级

Nestjs 多通关系:更改时更新父级,nestjs,typeorm,Nestjs,Typeorm,在我的NestJS项目中,我有以下具有一对一/多对一关系的实体: faculty.entity.ts: @OneToMany( type => Society, society => society.faculty, { eager: false }) societies: Society[]; @ManyToOne( type => Faculty, faculty => faculty.societies, { onDelete: 'CASCADE' }) @Joi

在我的NestJS项目中,我有以下具有一对一/多对一关系的实体:

faculty.entity.ts:

@OneToMany(
type => Society,
society => society.faculty,
{ eager: false })
societies: Society[];
@ManyToOne(
type => Faculty,
faculty => faculty.societies,
{ onDelete: 'CASCADE' })
@JoinColumn()
faculty: Faculty;

@Column()
facultyId: number;
society.entity.ts:

@OneToMany(
type => Society,
society => society.faculty,
{ eager: false })
societies: Society[];
@ManyToOne(
type => Faculty,
faculty => faculty.societies,
{ onDelete: 'CASCADE' })
@JoinColumn()
faculty: Faculty;

@Column()
facultyId: number;
我想实现一个功能来改变一个社会注定要拥有的能力

因此,在societies.service.ts中,我使用了补丁方法:

async patch(id: number, patchSocietyDto: PatchSocietyDto): Promise<Society> {
  const society = await this.findOne(id);

  for (const prop in patchSocietyDto) society[prop] = patchSocietyDto[prop];

  try {
    await society.save();
  } catch (err) {
    throw new InternalServerErrorException(err);
  }

  return society;
}
异步补丁(id:number,patchSocietyDto:patchSocietyDto):承诺{
const society=等待此消息。findOne(id);
for(patchSocietyDto中的const prop)society[prop]=patchSocietyDto[prop];
试一试{
等待社会的到来;
}捕捉(错误){
抛出新的InternalServerErrorException(err);
}
回归社会;
}
我想在请求正文中传递“facultyId”属性,删除现有关系,然后在id为“facultyId”的教员之间添加一个新关系


我怎样才能做到这一点呢?

我终于找到了解决办法。我唯一需要更改的是在一对多关系中将“渴望”选项设置为true:

@OneToMany(
  type => Society,
  society => society.faculty,
  { eager: true },
)
societies: Society[];
通过此更改,关系将通过传递“facultyId”属性的新值进行更新