From aabc6812456e960dfe1f05639ebeaaaa7b3a3576 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sat, 20 May 2023 21:31:27 +0800 Subject: [PATCH] fix: append belongs to association with fields (#1894) --- .../core/database/src/__tests__/repository/find.test.ts | 9 +++++++++ .../database/src/eager-loading/eager-loading-tree.ts | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/__tests__/repository/find.test.ts b/packages/core/database/src/__tests__/repository/find.test.ts index 310b88376..7f5520f94 100644 --- a/packages/core/database/src/__tests__/repository/find.test.ts +++ b/packages/core/database/src/__tests__/repository/find.test.ts @@ -440,6 +440,15 @@ describe('repository find', () => { expect(Object.keys(data)).toEqual(['id', 'posts']); expect(Object.keys(data['posts'])).not.toContain('id'); }); + + test('find one with appends', async () => { + const profile = await Profile.repository.findOne({ + filterByTk: 1, + appends: ['user.name'], + }); + + expect(profile.get('user').get('name')).toEqual('u1'); + }); }); describe('find', () => { diff --git a/packages/core/database/src/eager-loading/eager-loading-tree.ts b/packages/core/database/src/eager-loading/eager-loading-tree.ts index 496dd9681..a2182d136 100644 --- a/packages/core/database/src/eager-loading/eager-loading-tree.ts +++ b/packages/core/database/src/eager-loading/eager-loading-tree.ts @@ -76,10 +76,10 @@ export class EagerLoadingTree { } if (associationType == 'BelongsTo') { - const { sourceKey, foreignKey } = association; + const { targetKey, foreignKey } = association; pushAttribute(eagerLoadingTreeParent, foreignKey); - pushAttribute(child, sourceKey); + pushAttribute(child, targetKey); } eagerLoadingTreeParent.children.push(child);