【发布时间】:2021-01-04 14:38:01
【问题描述】:
我正在尝试与nestjs、typeorm 和postgres 实现一对一的关系。
我有一个配置文件实体
@OneToOne(() => Trainer, trainer => trainer.profile)
@JoinColumn({ name: 'trainer_trainer_id' })
trainer!: Trainer;
和一个培训师实体
@OneToOne(() => Profile, profile => profile.trainer)
profile!: Profile;
当用户提交成为 Trainer 时,我想用 trainer 表中的新行更新数据库,并在 profile 表中添加关联的 id。
所以我在训练库中这样做:
const trainer = CreateTrainerDto.toEntity(createTrainerDto);
trainer.profile = profile;
return await trainer.save();
这很好用,但每次都会在trainer 表中创建一个新行(并相应地更新profile 表中的id)。但我期待save() 更新现有行,如果已经保存了培训师。
我做错了吗?
【问题讨论】:
标签: nestjs typeorm one-to-one