-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relationships not updated correctly #9
Comments
Hey @Jcalcaldev thanks for the feedback. I'm extremely occupied these days. Will look into it after March 1st. |
I'm not pretty sure whether the I do understand that it might be confusing at the data level, since the entity is in fact soft-deleted but its Please let me know if there are any other troubles caused by this behavior. |
Since this behavior is acceptable (marking only parent entity as deleted) for REST API in GraphQL you need to soft delete even child entities, since you can go from child entity to parent entity in GraphQL query, which when parent is soft deleted can lead to unpredictable behavior. I managed to find a workaround for this using orphan removal. Example: class ParentEntity{
//some properties
@OneToMany(() => ChildEntity, (childEntity) => childEntity.parentEntity, { orphanRemoval: true })
childEntities: ChildEntity[]
}
class ChildEntity{
//some properties
@ManyToOne(() => ParentEntity)
parentEntity: ParentEntity
} then in your repo you can do something like this async removeParent(id: string) {
const parent = await this.parentRepo.findOne({ id } , {
populate: ['*'] // * symbol populates all relations (even nested), allowing for orphan removal to work properly
});
this.em.remove(parent);
await this.flush();
} The above code will soft delete all child entities of parent entity and child entities of child entities an so on This works, because orphan removal is triggered by the ORM and not DBMS allowing this library to work correctly. e.g. instead of populate: ['*'] use populate: ['childEntites.anotherNestedEntity'] For more information on this matter I highly recommend looking at this discussion, where author of MikroORM explains it I'm aware that this isn't the optimal solution, but rather a hacky workaround but it works |
I have a Foo entity with a OneToOne relation to a Bar entity.
When hard-deleting Foo, Bar is deleted due to having deleteRule: 'cascade'.
When soft-deleting Foo, Bar is not deleted and it's
deletedAt
property is not updated, only Foo'sdeletedAt
.HOWEVER: If I attempt to fetch Bar, the orm doesn't find it, even when its
deletedAt
property is still undefined. If I disable filters, it's fetched correctly.So it seems it's working half way. Am I missing something? Is there a standardized way I should be cascading soft deletions? I'm only using deleteRule: 'cascade'. No orphan removal.
The text was updated successfully, but these errors were encountered: