From d45623ee2eda551fa8e873008254f6505743f1e7 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Fri, 14 Oct 2022 17:29:14 +0800 Subject: [PATCH] fix: appends merge now using primary key (#911) * fix: appends merge now using primary key * chore: console.log --- packages/core/database/src/utils.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/utils.ts b/packages/core/database/src/utils.ts index a0aa8a4ac..2d515aa46 100644 --- a/packages/core/database/src/utils.ts +++ b/packages/core/database/src/utils.ts @@ -10,6 +10,8 @@ type HandleAppendsQueryOptions = { export async function handleAppendsQuery(options: HandleAppendsQueryOptions) { const { templateModel, queryPromises } = options; + const primaryKey = templateModel.constructor.primaryKeyAttribute; + const results = await Promise.all(queryPromises); let rows: Array; @@ -35,10 +37,19 @@ export async function handleAppendsQuery(options: HandleAppendsQueryOptions) { } for (let i = 0; i < appendedResult.rows.length; i++) { + const appendingRow = appendedResult.rows[i]; const key = appendedResult.include.association; - const val = appendedResult.rows[i].get(key); + const val = appendingRow.get(key); - rows[i].set(key, val, { + const rowKey = appendingRow.get(primaryKey); + + const targetIndex = rows.findIndex((row) => row.get(primaryKey) === rowKey); + + if (targetIndex === -1) { + throw new Error('target row not found'); + } + + rows[targetIndex].set(key, val, { raw: true, }); }