diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index 1d4a1e02f1..473a10df3d 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -692,6 +692,22 @@ export default class Column implements ColumnType { }, }, ); + + // if custom relation then delete + if ( + links.every((lk) => { + try { + return parseMetaProp(lk)?.custom; + } catch { + // ignore + } + }) + ) { + for (const link of links) { + await Column.delete(link.id, ncMeta); + } + } + if (links.length) { NcError.columnAssociatedWithLink(id); } diff --git a/packages/nocodb/src/services/tables.service.ts b/packages/nocodb/src/services/tables.service.ts index adedb45d3d..9e8a8b82f9 100644 --- a/packages/nocodb/src/services/tables.service.ts +++ b/packages/nocodb/src/services/tables.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import {Injectable} from '@nestjs/common'; import DOMPurify from 'isomorphic-dompurify'; import { AppEvents, @@ -11,28 +11,28 @@ import { RelationTypes, UITypes, } from 'nocodb-sdk'; -import { MetaDiffsService } from './meta-diffs.service'; -import { ColumnsService } from './columns.service'; +import {MetaDiffsService} from './meta-diffs.service'; +import {ColumnsService} from './columns.service'; import type { ColumnType, NormalColumnRequestType, TableReqType, UserType, } from 'nocodb-sdk'; -import type { MetaService } from '~/meta/meta.service'; -import type { LinkToAnotherRecordColumn, User, View } from '~/models'; +import type {MetaService} from '~/meta/meta.service'; +import type {LinkToAnotherRecordColumn, User, View} from '~/models'; import type { NcContext, NcRequest } from '~/interface/config'; -import { Base, Column, Model, ModelRoleVisibility } from '~/models'; -import { AppHooksService } from '~/services/app-hooks/app-hooks.service'; +import {Base, Column, Model, ModelRoleVisibility} from '~/models'; +import {AppHooksService} from '~/services/app-hooks/app-hooks.service'; import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2'; -import { NcError } from '~/helpers/catchError'; +import {NcError} from '~/helpers/catchError'; import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT'; import getColumnUiType from '~/helpers/getColumnUiType'; -import getTableNameAlias, { getColumnNameAlias } from '~/helpers/getTableName'; +import getTableNameAlias, {getColumnNameAlias} from '~/helpers/getTableName'; import mapDefaultDisplayValue from '~/helpers/mapDefaultDisplayValue'; import Noco from '~/Noco'; import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2'; -import { sanitizeColumnName, validatePayload } from '~/helpers'; +import {sanitizeColumnName, validatePayload} from '~/helpers'; import { getUniqueColumnAliasName, getUniqueColumnName, @@ -44,7 +44,8 @@ export class TablesService { protected readonly metaDiffService: MetaDiffsService, protected readonly appHooksService: AppHooksService, protected readonly columnsService: ColumnsService, - ) {} + ) { + } async tableUpdate( context: NcContext, @@ -212,9 +213,9 @@ export class TablesService { return ( isLinksOrLTAR(c) && (c.colOptions as LinkToAnotherRecordColumn).type === - RelationTypes.MANY_TO_MANY && + RelationTypes.MANY_TO_MANY && (c.colOptions as LinkToAnotherRecordColumn).fk_mm_model_id === - table.id + table.id ); }); }); @@ -226,6 +227,19 @@ export class TablesService { ); } + // if table is using in custom relation as junction table then delete all the relation + const relations = await Noco.ncMeta.metaList2(null, null, MetaTable.COL_RELATIONS, { + condition: { + fk_mm_model_id: table.id, + }, + }); + + if (relations?.data?.list?.length) { + for (const relation of relations.data.list) { + await Column.delete(relation.fk_column_id); + } + } + const base = await Base.getWithInfo(context, table.base_id); const source = base.sources.find((b) => b.id === table.source_id); @@ -356,7 +370,7 @@ export class TablesService { 'guest', ]; - const defaultDisabled = roles.reduce((o, r) => ({ ...o, [r]: false }), {}); + const defaultDisabled = roles.reduce((o, r) => ({...o, [r]: false}), {}); let models = _models || @@ -380,7 +394,7 @@ export class TablesService { _tn: view.title, table_meta: model.meta, ...view, - disabled: { ...defaultDisabled }, + disabled: {...defaultDisabled}, }; } @@ -503,7 +517,7 @@ export class TablesService { if (!col || !col.system) { tableCreatePayLoad.columns.push({ - ...(await getColumnPropsFromUIDT({ uidt } as any, source)), + ...(await getColumnPropsFromUIDT({uidt} as any, source)), column_name: colName, cn: colName, title: colAlias, @@ -681,9 +695,9 @@ export class TablesService { let columns: Array< Omit & { - cn: string; - system?: boolean; - } + cn: string; + system?: boolean; + } >; if (!source.isMeta()) {