Git Product home page Git Product logo

Comments (8)

B4nan avatar B4nan commented on June 7, 2024 2

Yeah, for sure, I will include the repro from OP.

from mikro-orm.

hesalx avatar hesalx commented on June 7, 2024 1

@ashleyww93 You can override .getRepository() with a custom EM to be able to get visibility into any occurence of this specific issue. Feel free to just log instead of a hard error.

// replace `PostgreSqlDriver` with you respective driver
export class ExtendedEntityManager extends EntityManager<PostgreSqlDriver> {
  public getRepository<Entity extends object>(entityName: EntityName<Entity>) {
    const repository = super.getRepository(entityName);
    const currentContext = this.id;
    const repoContext = repository.getEntityManager().id;
    if (currentContext !== repoContext) {
      throw new Error(
        `EntityRepository<${repository.getEntityName()}> context (${repoContext}) did not match the current EntityManager context (${currentContext})!`,
      );
    }
    return repository as any;
  }
}
defineConfig({
  // ...
  entityManager: ExtendedEntityManager,
});

Docs: https://mikro-orm.io/docs/entity-manager#extending-entitymanager

from mikro-orm.

hesalx avatar hesalx commented on June 7, 2024 1

@B4nan Awesome! Thanks.

Any chance the test suite can be amended with a case like this?

await em.transactional(async _ => {
    // first even call to a repository, em is not tx-specific
    await em.getRepository(Post).find({id: 1});
});
assert.equal(em.id, em.getRepository(Post).getEntityManager().id)

from mikro-orm.

B4nan avatar B4nan commented on June 7, 2024

That's funny, the commit that broke this (af87693) was trying to fix the exact same error (#3074). Will need to reread that thread, as reverting this would probably break some other case...

from mikro-orm.

hesalx avatar hesalx commented on June 7, 2024

It is worth noting that the aforementioned af87693 explicitly states "do not use context in em.getRepository()", however it is still used there (v6.1.12) and was added by b5b03a6#diff-a62cd78c400d99253a3c0fb5a9bf26e1e63e0deb511059d28ba51014b58242ddR146 but the change does not seem related to the commit's intention.

from mikro-orm.

ashleyww93 avatar ashleyww93 commented on June 7, 2024

Could this be causing other unintended consequences?

I’m currently trying to track down an issue(so I can make a reproduction) where entities are deleted and recreated with the same ID.

I thought it was an issue our side (and might be), but it’s seemingly random, like the identity map is incorrect on some occasions.

from mikro-orm.

hesalx avatar hesalx commented on June 7, 2024

@ashleyww93 If this issue is the culprit of your problem, it may appear random because it requires the first .getRepository() call in the app process to be inside a transaction. After a server restart it may be completely up to chance whether it happens this way.

from mikro-orm.

B4nan avatar B4nan commented on June 7, 2024

It is worth noting that the aforementioned af87693 explicitly states "do not use context in em.getRepository()", however it is still used there (v6.1.12) and was added by b5b03a6#diff-a62cd78c400d99253a3c0fb5a9bf26e1e63e0deb511059d28ba51014b58242ddR146 but the change does not seem related to the commit's intention.

Yeah correct, and reverting this bit is not making any of the existing tests fail, I guess I will go with that, worst case we will revert it, but I agree it looks unrelated to the commit. That change also makes the e2e test suite of the real-world app pass, so it feels safe.

from mikro-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.