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
I've encountered an issue while trying to persist a new entity with one-to-one relationship with another, already persisted entity, using getReference() method to obtain reference to already-persisted side of 1:1 relationship.
and returns carId. A separate repository persists Driver entity and tries to create 1:1 relationship at that time:
async persistDriverForCar(body: { driverName: string; carId: string }) {
const record = new DriverEntity();
record.name = body.driverName;
const car = this.em.getReference(CarEntity, body.carId);
record.car = car;
this.em.persist(car);
await this.em.flush();
}
the getReference method successfully obtains reference to previously-persisted Car entity in both 5.7.7 and 5.7.8. The generated SQL, however, refers to the Car entity successfully in 5.7.7 but doesn't in 5.7.8.
Expected behavior
Persisting new entity with reference to another entity obtained via getReference method should allow for persistence of 1:1 relationship.
Additional context
Thank you so much for your amazing work on MikroORM!
Repro code is a bit of an example - I don't normally import domain modules to another modules in NestJS and use EventEmitter but I've done that here to simplify.
I do wonder if perhaps I was doing something against Mikro-ORM that I shouldn't have been but wanted to let you know in case this is an unintentional breaking change.
Versions
See package.json in repro.
Dependency
Version
node
20.1.0
typescript
5.1.3
mikro-orm
5.7.8 (works ?as intended on 5.7.7)
your-driver
postgresql
The text was updated successfully, but these errors were encountered:
Could you please turn this into a script instead of a nestjs app? Doesn't have to be a test case per se, but something I can just run and see it fail, instead of starting a web server and doing requests to it.
Repro script works fine with SQLite but issue is reproduced with MikroORM 5.7.12 and postgres driver on pg15 (compose file in repo).
test('persisting 1:1 relation issue',async()=>{constem=orm.em.fork();letcar=em.create(CarEntity,{brand: 'skoda'});awaitem.persist(car).flush();constcarReference=em.getReference(CarEntity,car.id);letdriver=em.create(DriverEntity,{name: 'John Doe',car: carReference});constdriverId=driver.id;awaitem.persist(driver).flush();constmanagedCar=car;expect(managedCar.driver).toBeDefined();expect(managedCar.driver.id).toStrictEqual(driverId);// Driver is defined and has an id in managed entityconstfoundCar=awaitem.findOneOrFail(CarEntity,{id: car.id});expect(foundCar).toBeDefined();expect(foundCar.driver).not.toBeNull();// Fails, driver is null in found entity});
What I got so far is:
Seems to be introduced with #7127ff6, #b5ab66bc works fine
If entity type doesn't have a version field, it works fine
@Property({version: true})version!: number;/// If this is commented out, issue is not reproduced
I've been looking into the internals of MikroORM and would love to try and offer a fix but I'm failing thus far - do let me know if there is anything else I can do.
Describe the bug
I've encountered an issue while trying to persist a new entity with one-to-one relationship with another, already persisted entity, using getReference() method to obtain reference to already-persisted side of 1:1 relationship.
I experienced this on update from 5.7.7 to 5.7.8.
To Reproduce
Repro repository link: https://github.com/merkator95/mikro-orm-entity-manager-get-reference-repro
Application manages two entities: Car and Driver. There is 1:1 relationship between those achieved via:
a controller accepts Car brand and Car driver name as follows:
A Car repository in its own class persists Car entity:
and returns carId. A separate repository persists Driver entity and tries to create 1:1 relationship at that time:
the getReference method successfully obtains reference to previously-persisted Car entity in both 5.7.7 and 5.7.8. The generated SQL, however, refers to the Car entity successfully in 5.7.7 but doesn't in 5.7.8.
Expected behavior
Persisting new entity with reference to another entity obtained via getReference method should allow for persistence of 1:1 relationship.
Additional context
Thank you so much for your amazing work on MikroORM!
Repro code is a bit of an example - I don't normally import domain modules to another modules in NestJS and use EventEmitter but I've done that here to simplify.
I do wonder if perhaps I was doing something against Mikro-ORM that I shouldn't have been but wanted to let you know in case this is an unintentional breaking change.
Versions
See package.json in repro.
The text was updated successfully, but these errors were encountered: