Skip to content

Commit

Permalink
fix(core): try to fix merging of large collections loaded via joined …
Browse files Browse the repository at this point in the history
…strategy

Closes #4694
  • Loading branch information
B4nan committed Sep 12, 2023
1 parent 5e974a9 commit faae84e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/entity/EntityLoader.ts
Expand Up @@ -234,14 +234,20 @@ export class EntityLoader {
if (mapToPk) {
children.forEach(child => {
const pk = child.__helper.__data[prop.mappedBy] ?? child[prop.mappedBy];
const key = helper(this.em.getReference(prop.type, pk)).getSerializedPrimaryKey();
map[key].push(child as T);

if (pk) {
const key = helper(this.em.getReference(prop.type, pk)).getSerializedPrimaryKey();
map[key].push(child as T);
}
});
} else {
children.forEach(child => {
const entity = child.__helper.__data[prop.mappedBy] ?? child[prop.mappedBy];
const key = helper(entity).getSerializedPrimaryKey();
map[key].push(child as T);

if (entity) {
const key = helper(entity).getSerializedPrimaryKey();
map[key].push(child as T);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Expand Up @@ -780,7 +780,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
if (map[pk]) {
joinedProps.forEach(hint => {
if (Array.isArray(map[pk][hint.field]) && Array.isArray(item[hint.field])) {
map[pk][hint.field].push(...item[hint.field]);
item[hint.field].forEach((el: T) => map[pk][hint.field].push(el));
}
});
} else {
Expand Down
Expand Up @@ -114,7 +114,7 @@ describe('adding m:1 with composite PK (FK as PK + scalar PK) (GH 1687)', () =>
beforeAll(async () => {
orm = await MikroORM.init({
entities: [City, User, Country, State],
dbName: `mikro_orm_test_gh_1687`,
dbName: `mikro_orm_test_composite_fks`,
driver: PostgreSqlDriver,
});
await orm.schema.ensureDatabase();
Expand Down

0 comments on commit faae84e

Please sign in to comment.