【发布时间】:2022-11-01 17:26:14
【问题描述】:
我将 NestJS 与 mikro-Orm 一起使用,并且对我的所有 manyToMany 关系都有一个奇怪的行为。
@ObjectType()
@Entity()
export class Realty {
@Field(() => ID)
@PrimaryKey({ columnType: "uuid" })
id: string = v4();
@Field(() => [Contact])
@ManyToMany(() => Contact, (contact) => contact.realties)
contacts: Collection<Contact>;
}
@ObjectType()
@Entity()
export class Contact {
@Field(() => ID)
@PrimaryKey({ columnType: "uuid" })
id: string = v4();
@Field(() => [Realty])
@ManyToMany(() => Realty, (realty) => realty.contacts, { owner: true })
realties: Collection<Realty>;
}
当我想从联系人中删除 realtyReference 时,效果很好,并且 Contact_Realty PivotTable 中的行被删除。但是,当我尝试从不动产中删除 contactReference 时,什么也没有发生。这只对拥有方有效吗?
ContactsService(工作):
async update(updateContactInput: UpdateContactInput) {
const { id, realtyIds } = updateContactInput;
const contact = await this.findOneOrFail(id);
const updated = this.contactsRepository.assign(contact, {
realties: await this.realtiesService.find(realtyIds),
});
await this.contactsRepository.persistAndFlush(updated);
return updated;
}
房地产服务(返回正确的更新实体,但不删除数据透视表中的行):
async update(updateRealtyGeneralInput: UpdateRealtyGeneralInput) {
const { id, contactIds } = updateRealtyGeneralInput;
const realty = await this.realtiesService.findOneOrFail(id);
const updated = this.realtiesRepository.assign(realty, {
contacts: await this.contactsService.find(contactIds),
});
await this.realtiesRepository.persistAndFlush(updated);
return updated;
}
两者都返回正确的更新实体,但只有 ContactsService 实际删除了数据透视表中的行。
非常感谢一些帮助,非常感谢!
我想从不动产中删除一个或多个联系人,但无法使其正常工作。难道我做错了什么?
【问题讨论】:
标签: orm many-to-many mikro-orm