You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Deleting a child entity with a relation to parent X, then persisting a new child entity with a relation to the same parent causes the removeStack to be emptied, and em.flush() to error.
I load a parent (OneToMany) and its children (ManyToOne) from the database:
const parent = new Parent().assign(
{ id: "parentId" },
{ em }
);
em.persist(parent);
const child1 = new Child().assign(
{ id: "childId1", parent },
{ em }
);
em.persist(child1);
const child2 = new Child().assign(
{ id: "childId2", parent },
{ em }
);
em.persist(child2);
await em.flush();
Query created data, delete a child, create and persist a new child
const storedParent = await em.findOneOrFail(Parent, "parentId", {
populate: ["children"],
});
const removeChild = storedParent.children[0]
// Remove child
em.remove(removeChild)
// Remove stack gets + 1 element
console.log(em.getUnitOfWork().getRemoveStack().size);
// Add unrelated child to same parent
const newChild = new Child().assign(
{
id: "newChildId",
parent: storedParent,
}
{ em }
);
// Remove stack size is still 1
console.log(em.getUnitOfWork().getRemoveStack().size);
// Persist
em.persist(newChild);
// Remove stack is empty after persist
console.log(em.getUnitOfWork().getRemoveStack().size);
// Flushing errors on trying to create a child with the id of the removed child
em.flush()
Current fix: explicitly removing the child from the parent as well, but this feels unintuitive.
Expected behavior
I would expect that a child can be added (persisted) to a parent, after another child has been deleted from the same parent using em.remove(child)
Versions
Dependency
Version
node
12.16.2
typescript
3.9.7
mikro-orm
4.2.1
postgres
12
The text was updated successfully, but these errors were encountered:
Might be issue with the assign helper, it is meant for updating existing entities, not really for inserting new entities. Better to use em.create() (it will call the ctor for you, with the right parameters).
But the issue will be probably cascade persist, as persisting the child is then cascaded to the parent property, and persisting an entity marked for removal will clear the entity from remove stack automatically.
edit: I can confirm the behaviour you described (as well as that em.create() won't help here). Thanks for the repro with detailed description! Only if all the bug reports looked like this 👍
Describe the bug
Deleting a child entity with a relation to parent X, then persisting a new child entity with a relation to the same parent causes the removeStack to be emptied, and em.flush() to error.
I load a parent (OneToMany) and its children (ManyToOne) from the database:
I delete one of the children using em.remove():
When I persist a new Child entity with a relation to the Parent entity, the removeStack is emptied:
After this, em.flush() errors on duplicate primary key as it tries to insert the removed child:
To Reproduce
Entity definition:
Create intial dataset:
Query created data, delete a child, create and persist a new child
Current fix: explicitly removing the child from the parent as well, but this feels unintuitive.
Expected behavior
I would expect that a child can be added (persisted) to a parent, after another child has been deleted from the same parent using em.remove(child)
Versions
The text was updated successfully, but these errors were encountered: