Skip to content

Removing an entity from a collection does not affect records in a transaction #3360

@mrmlnc

Description

@mrmlnc

📖 I'm not sure if the current behavior is a bug, but the behavior is not obvious to me.

Describe the bug

I have an entity with a 1:M relation. For this relation I'm set the orphanRemoval: true option. If I delete an item from a collection and then call the em.persistAndFlush method in the transaction, then the items will not be deleted from the database.

entity.children.removeAll();

await orm.em.transactional(async (em) => {
    // Works fine
    // entity.children.removeAll();

    // Simplified code. There are several operations.
    await em.persistAndFlush(entity);
});

📖 I have a repository that makes it easy to reproduce the problem:
https://github.com/mrmlnc/mikro-orm-collection-transaction-issue

To Reproduce
Steps to reproduce the behavior:

  1. git clone https://github.com/mrmlnc/mikro-orm-collection-transaction-issue
  2. npm ci
  3. npm run start

Expected behavior

Items removed from the collection will be removed from the database.

Additional context

I found that the problem disappears if you do a deletion inside a transaction. I think the current problem is related to copying the current state into the fork for transaction.

Versions

Dependency Version
node 16.16.0
typescript 4.7.4
mikro-orm 5.3.0
better-sqlite 5.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions