Comments (8)
Yeah, for sure, I will include the repro from OP.
from mikro-orm.
@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.
@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.
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.
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.
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.
@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.
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)
- Entity Generator is extremely slow on large databases and has bugs HOT 13
- Maximum call stack exceed with double $none operator HOT 1
- I want to use mixin as base class but mikroorm dedup the base class by name
- Make the `getSingleResult()` add limit to the query
- Updating a Json field in postgresql does not remove properties from the json in the DB HOT 1
- Unknown behavior: SynctaxError: Unexpected number at new Function (<anonymous>)
- Order by doesn’t work with embeddable fields inside populated entity
- Feat: Ability to add migrations without actually running it HOT 10
- There is an issue when attempting to fetch an entity by referencing a field within an 'Embeddable' object that belongs to a related entity
- Get Change Sets reports changes in embeddable entity with no changes HOT 1
- Naming errors in Entity Generator HOT 4
- DriverException when using pessimistic LockMode in combination with populate
- In-memory DB compatible with MikroORM and NestJS for integration testing (API endpoints)
- Updating JSON fields with another JSON structure, merges with existing structure instead of replacing the old one
- this.driver.getPlatform is not a function HOT 1
- Minor type issue: em.fork does not use entityManager type set by MikroORM.init HOT 1
- Issues getting populate hint typechecking to work HOT 3
- CLI with NodeNext module/moduleResolution
- I can't find the correct type for query
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mikro-orm.