Skip to content

Commit

Permalink
fix(core): fix extra updates with select-in strategy and composite FKs
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Dec 20, 2023
1 parent cf7cee1 commit c848f8c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/entity/EntityHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class EntityHelper {

// when propagation from inside hydration, we set the FK to the entity data immediately
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
wrapped.__originalEntityData[prop.name as string] = helper(wrapped.__data[prop.name]).getPrimaryKey(true);
wrapped.__originalEntityData[prop.name as string] = Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta!.primaryKeys, true);
} else {
wrapped.__touched = !hydrator.isRunning();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export class EntityMetadata<T = any> {

// when propagation from inside hydration, we set the FK to the entity data immediately
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
wrapped.__originalEntityData[prop.name as string] = val.__helper.getPrimaryKey(true);
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(val, prop.targetMeta!.primaryKeys, true);
} else {
wrapped.__touched = !hydrator.isRunning();
}
Expand Down
9 changes: 4 additions & 5 deletions tests/issues/GHx9.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,22 @@ afterAll(async () => {
await orm.close();
});

test('extra updates with 1:1 relations (joined)', async () => {
test('extra updates with 1:1 relations (select-in)', async () => {
const result = await orm.em.findOneOrFail(Project, { id: project.id, organization: org.id }, {
populate: ['projectUpdate'],
strategy: LoadStrategy.JOINED,
strategy: LoadStrategy.SELECT_IN,
});

expect(wrap(result.projectUpdate!.$).isTouched()).toBe(false);
expect(wrap(result).isTouched()).toBe(false);

orm.em.getUnitOfWork().computeChangeSets();
expect(orm.em.getUnitOfWork().getChangeSets()).toHaveLength(0);
});

test('extra updates with 1:1 relations (select-in)', async () => {
test('extra updates with 1:1 relations (joined)', async () => {
const result = await orm.em.findOneOrFail(Project, { id: project.id, organization: org.id }, {
populate: ['projectUpdate'],
strategy: LoadStrategy.SELECT_IN,
strategy: LoadStrategy.JOINED,
});

expect(wrap(result.projectUpdate!.$).isTouched()).toBe(false);
Expand Down

0 comments on commit c848f8c

Please sign in to comment.