【发布时间】:2019-04-10 18:34:15
【问题描述】:
我正在将 mikro-orm 与 MongoDB 一起使用并尝试执行 Cascade.REMOVE,但我无法让它工作。
企业实体:
@Entity({ collection: "business" })
export class BusinessModel implements BusinessEntity {
@PrimaryKey()
public _id!: ObjectId;
@Property()
public name!: string;
@Property()
public description!: string;
@OneToMany({ entity: () => LocationModel, fk: "business", cascade: [Cascade.ALL] })
public locations: Collection<LocationModel> = new Collection(this);
}
export interface BusinessModel extends IEntity<string> { }
位置实体:
@Entity({ collection: "location" })
export class LocationModel implements LocationEntity {
@PrimaryKey()
public _id!: ObjectId;
@Property()
public geometry!: GeometryEmbedded;
@ManyToOne({ entity: () => BusinessModel})
public business!: BusinessModel;
public distance?: number;
}
export interface LocationModel extends IEntity<string> { }
业务数据:
_id: 5cac818273243d1439866227
name: "Prueba"
description: "Prueba eliminacion"
位置数据:
_id: 5cac9807179e380df8e43b6c
geometry: Object
business: 5cac818273243d1439866227
_id: 5cacc941c55fbb0854f86939
geometry: Object
business: 5cac818273243d1439866227
还有代码:
export default class BusinessData {
private businessRepository: EntityRepository<BusinessModel>;
public constructor(@inject(OrmClient) ormClient: OrmClient) {
this.businessRepository = ormClient.em.getRepository(BusinessModel);
}
public async delete(id: string): Promise<number> {
return await this.businessRepository.remove(id);
}
}
“企业”已正确删除,但所有相关“位置”仍保留在那里。
日志只显示:
[query-logger] db.getCollection("business").deleteMany() [耗时 0 ms]
【问题讨论】:
-
只是好奇 - 你真的从查询记录器中得到了这个,还是你删除了 where 部分?如果是这样,您确定要为
id参数提供值吗? -
不,我没有删除 where 部分,我为 id 参数提供了值。将代码更改为:
public async delete(id: string): Promise<number> { // tslint:disable-next-line: no-console console.log(id); return await this.businessRepository.remove(id); }这是实体发现后的输出,不删除任何内容:5cac818273243d1439866227 [query-logger] db.getCollection("business").deleteMany() [took 0 ms] -
刚查了代码,只在查询记录器中丢失,查询数据库时没有省略条件。会尽快解决的,看起来很吓人:]
标签: mikro-orm