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
Describe the bug
If entity has embedded property without object: true option then it breaks UnitOfWork / IdentityMap - multiple calls to repository.findOne(PK) always trigger SQL queries despite entity should be "cached" in IdentityMap.
To Reproduce
Entity classes:
// TestEntity1 classexportclassTestEntity1{publicreadonlyid: string;publicreadonlycustomProp: CustomProp;constructor(id: string,customProp: CustomProp){this.id=id;this.customProp=customProp;}}// TestEntity2 class (identical to TestEntity1)exportclassTestEntity2{publicreadonlyid: string;publicreadonlycustomProp: CustomProp;constructor(id: string,customProp: CustomProp){this.id=id;this.customProp=customProp;}}// CustomProp classexportclassCustomProp{publicreadonlysomeValue: string;constructor(someValue: string){this.someValue=someValue;}}
Entity schemas:
// TestEntity1 schemaexportconstTestEntity1Schema=newEntitySchema({class: TestEntity1,properties: {id: {type: 'text',primary: true,},customProp: {type: 'CustomProp',nullable: false,reference: ReferenceType.EMBEDDED,object: true,// <-------------------------------- true},},});// TestEntity2 schemaexportconstTestEntity2Schema=newEntitySchema({class: TestEntity2,properties: {id: {type: 'text',primary: true,},customProp: {type: 'CustomProp',nullable: false,reference: ReferenceType.EMBEDDED,object: false,// <-------------------------------- false (this is the only difference between those schemas)},},});// CustomProp schemaexportconstCustomPropSchema=newEntitySchema({class: CustomProp,embeddable: true,properties: {someValue: {type: 'text',nullable: false,},},});
// Insert entities (native inserts skip IdentityMap)awaitthis.testEntity1Repository.nativeInsert(newTestEntity1('abc',newCustomProp('yyy')),);awaitthis.testEntity2Repository.nativeInsert(newTestEntity2('def',newCustomProp('xxx')),);// Find TestEntity1 twice by PKawaitthis.testEntity1Repository.findOne('abc');awaitthis.testEntity1Repository.findOne('abc');// should not trigger SQL query// Find TestEntity2 twice by PKawaitthis.testEntity2Repository.findOne('def');awaitthis.testEntity2Repository.findOne('def');// should not trigger SQL query// Remove entities (just for cleanup)this.testEntity1Repository.remove(e1!);this.testEntity2Repository.remove(e2!);
Current behavior
[query] insert into "test_entity1" ("id", "custom_prop") values ('abc', '{"someValue":"yyy"}') returning "id" [took 4 ms]
[query] insert into "test_entity2" ("id", "custom_prop_some_value") values ('def', 'xxx') returning "id" [took 4 ms]
# TestEntity1 is retrieved from DB once
[query] select"t0".* from "test_entity1" as "t0" where "t0"."id" = 'abc' limit 1 [took 2 ms, 1 result]
# TestEntity2 is retrieved from DB twice
[query] select"t0".* from "test_entity2" as "t0" where "t0"."id" = 'def' limit 1 [took 3 ms, 1 result]
[query] select"t0".* from "test_entity2" as "t0" where "t0"."id" = 'def' limit 1 [took 2 ms, 1 result]
[query] begin
[query] delete from "test_entity1" where "id"in ('abc') [took 2 ms]
[query] delete from "test_entity2" where "id"in ('def') [took 3 ms]
[query] commit
Expected behavior
In example above both entities should be retrieved from DB only once and cached in IdentityMap.
For some reason if entity has embedded property with object: false option then it is not cached in IdentityMap (or can't be found in IdentityMap not sure). Changing entity schema to use object: true fixes the issue, however then embedded property is of JSON type without properties of embedded entity mapped to separate DB table columns.
Versions
Dependency
Version
node
18.16.0
typescript
5.2.2
mikro-orm
5.9.3
mikro-orm/postgresql
5.9.3
The text was updated successfully, but these errors were encountered:
Got it, but do you think it can/should be fixed? Or maybe there is workaround to mark is as not partially loaded? I tried to add { populate: true } option to findOne() but it does not change anything.
Describe the bug
If entity has embedded property without
object: true
option then it breaks UnitOfWork / IdentityMap - multiple calls torepository.findOne(PK)
always trigger SQL queries despite entity should be "cached" in IdentityMap.To Reproduce
Entity classes:
Entity schemas:
Current behavior
Expected behavior
In example above both entities should be retrieved from DB only once and cached in IdentityMap.
For some reason if entity has embedded property with
object: false
option then it is not cached in IdentityMap (or can't be found in IdentityMap not sure). Changing entity schema to useobject: true
fixes the issue, however then embedded property is of JSON type without properties of embedded entity mapped to separate DB table columns.Versions
The text was updated successfully, but these errors were encountered: