From e4e9d2e7f12958b4a4398cb9975490152b1a7825 Mon Sep 17 00:00:00 2001 From: Raju Udava <86527202+dstala@users.noreply.github.com> Date: Thu, 9 May 2024 22:22:28 +0530 Subject: [PATCH] Nc fix/duplicate link (#8422) * fix: link duplicate (self reference) * fix: avoid iterating source twice * fix: duplicate self-referencing oo & hm * fix: self-referencing mm for tables with bt --------- Co-authored-by: Pranav C Co-authored-by: mertmit --- .../jobs/export-import/duplicate.processor.ts | 7 ++-- .../jobs/jobs/export-import/import.service.ts | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts b/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts index 013fcf9580..d2e73a834a 100644 --- a/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts +++ b/packages/nocodb/src/modules/jobs/jobs/export-import/duplicate.processor.ts @@ -352,7 +352,7 @@ export class DuplicateProcessor { .map((c) => c.id); if (bts.length > 0) { - fields[md.id] = [md.primaryKey.id]; + fields[md.id] = fields[md.id] ? fields[md.id] : [md.primaryKey.id]; fields[md.id].push(...bts); } } @@ -365,7 +365,10 @@ export class DuplicateProcessor { destBase: source, hrTime, modelFieldIds: fields, - externalModels: [sourceModel, ...relatedModels], + externalModels: [ + sourceModel, + ...relatedModels.filter((m) => m.id !== sourceModel.id), + ], }); elapsedTime(hrTime, 'import model data', 'duplicateColumn'); diff --git a/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts b/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts index 3789e52705..ea0ea99a49 100644 --- a/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts +++ b/packages/nocodb/src/modules/jobs/jobs/export-import/import.service.ts @@ -299,6 +299,11 @@ export class ImportService { colOptions.fk_mm_model_id && a.id !== col.id, ); + // referencing the same model + if (colOptions.fk_related_model_id === modelData.id) { + continue; + } + for (const nColumn of childModel.columns) { if ( nColumn?.colOptions?.fk_mm_model_id === @@ -386,6 +391,33 @@ export class ImportService { a.id !== col.id, ); + // referencing the same model + if (colOptions.fk_related_model_id === modelData.id) { + const counterRelationType = + colOptions.type === 'hm' ? 'bt' : 'oo'; + const oldCol = childModel.columns.find( + (oColumn) => + oColumn.colOptions?.fk_parent_column_id === + getEntityIdentifier(colOptions.fk_parent_column_id) && + oColumn.colOptions?.fk_child_column_id === + getEntityIdentifier(colOptions.fk_child_column_id) && + oColumn.colOptions?.type === counterRelationType, + ); + const col = childModel.columns.find( + (nColumn) => + nColumn.colOptions?.fk_parent_column_id === + getIdOrExternalId(colOptions.fk_parent_column_id) && + nColumn.colOptions?.fk_child_column_id === + getIdOrExternalId(colOptions.fk_child_column_id) && + nColumn.colOptions?.type === counterRelationType, + ); + idMap.set( + `${oldCol.base_id}::${oldCol.source_id}::${oldCol.fk_model_id}::${oldCol.id}`, + col.id, + ); + continue; + } + for (const nColumn of childModel.columns) { if ( nColumn.id !== getIdOrExternalId(col.id) &&