From 8e62601a2e22e535e356cc768a07e99900f15f79 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 12 Jul 2024 18:22:04 +0000 Subject: [PATCH] fix: show error while deleting column used in a custom relation --- packages/nocodb/src/db/BaseModelSqlv2.ts | 2 +- .../nocodb/src/services/columns.service.ts | 42 +++++++++++++++++++ .../nocodb/src/services/tables.service.ts | 2 +- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 8819059510..8effffb245 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -6936,7 +6936,7 @@ class BaseModelSqlv2 { delete item[key]; } }); - });k + }); return data; } diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index 33f3028a6e..8cb5d1a8a0 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -64,6 +64,7 @@ import Noco from '~/Noco'; import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2'; import { MetaTable } from '~/utils/globals'; import { MetaService } from '~/meta/meta.service'; +import { parseMetaProp } from '~/utils/modelUtils'; // todo: move export enum Altered { @@ -2099,6 +2100,47 @@ export class ColumnsService { ProjectMgrv2.getSqlMgr(context, { id: source.base_id }, ncMeta), ); + // check column association with any custom links or LTAR + if (!isVirtualCol(col)) { + const links = await ncMeta.metaList2( + null, + null, + MetaTable.COL_RELATIONS, + { + xcCondition: { + _or: [ + { fk_child_column_id: { eq: id } }, + { fk_parent_column_id: { eq: id } }, + { fk_mm_child_column_id: { eq: id } }, + { fk_mm_parent_column_id: { eq: id } }, + ], + }, + }, + ); + + // if custom relation then delete + if ( + links?.length && + links.every((lk) => { + try { + return parseMetaProp(lk)?.custom; + } catch { + // ignore + } + }) + ) { + const linkCol = await Column.get(links[0].fk_column_id, ncMeta); + const table = await linkCol.getModel(ncMeta); + NcError.columnAssociatedWithLink( + `Column is associated with custom link ${ + linkCol.title || linkCol.column_name + } (${ + table.title || table.table_name + }). Please delete the link column first.`, + ); + } + } + /** * @Note: When using 'falls through to default' cases in a switch statement, * it is crucial to place them after cases with break statements. diff --git a/packages/nocodb/src/services/tables.service.ts b/packages/nocodb/src/services/tables.service.ts index 4e63db4f72..dbd4dfc8a7 100644 --- a/packages/nocodb/src/services/tables.service.ts +++ b/packages/nocodb/src/services/tables.service.ts @@ -238,7 +238,7 @@ export class TablesService { }, ); - if (relations.leghth) { + if (relations.length) { const relCol = await Column.get(relations[0].fk_column_id); const relTable = await Model.get(relCol.fk_model_id); NcError.badRequest(