Skip to content
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

Upgrade to mikro-orm v5 #214

Open
alex996 opened this issue Apr 19, 2022 · 0 comments
Open

Upgrade to mikro-orm v5 #214

alex996 opened this issue Apr 19, 2022 · 0 comments

Comments

@alex996
Copy link

alex996 commented Apr 19, 2022

After migrating to mikro-orm v5 (from ^4.5.9), my integration tests are suddenly failing with the following error:

ValidationError: You cannot call em.flush() from inside lifecycle hook handlers
    at Function.cannotCommit (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/core/errors.js:77:16)
    at UnitOfWork.commit (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:168:44)
    at SqlEntityManager.flush (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/core/EntityManager.js:497:36)
    at SqlEntityManager.persistAndFlush (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/core/EntityManager.js:449:36)

The FAQ page states that this can happen when the request context is not set up properly. In my case, it is already set up:

app.use((req, res, next) => RequestContext.create(orm.em, next));

I refer to the Entity Manager like this: orm.em. This appears to be safe based on its description: The global EntityManager instance. If you are using RequestContext helper, it will automatically pick the request specific context under the hood. In other words, it doesn't seem necessary to call RequestContext.getEntityManager() manually - in fact, if I do, it has no effect on the error.

I also use ava which runs tests concurrently, each in its own sandbox. My test files start with a before-all hook which calls a setup function. This function creates an instance of Express that is then shared and invoked by every test case using supertest.

If my test file only contains a single test, it passes. However, as soon as I add a second test I get the above error. This is key - I can see that orm.em.persistAndFlush is indeed called twice - but it's called twice in separate concurrent requests, meaning RequestContext.create also gets invoked twice.

I noticed pg-mem is currently on ^4.5.3 of mikro. I also looked at the current adapter, but nothing stands out. As an experiment, if I manually replace pg-mem/node_modules/@mikro-orm with the v5 of node_modules/@mikro-orm, I get

TypeError {
    message: 'builder.generateDdlCommands is not a function',
  }

  › SchemaGenerator.dump (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/knex/schema/SchemaGenerator.js:447:35)
  › SchemaGenerator.getCreateSchemaSQL (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/knex/schema/SchemaGenerator.js:61:31)
  › SchemaGenerator.createSchema (/path/to/my/project/node_modules/pg-mem/node_modules/@mikro-orm/knex/schema/SchemaGenerator.js:26:32)

Could this by any chance be related to version mismatch? Thanks


package.json:

"@mikro-orm/core": "^5.1.2",
"@mikro-orm/postgresql": "^5.1.2",

"pg-mem": "^2.3.5",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant