diff --git a/packages/nocodb-sdk/src/lib/globals.ts b/packages/nocodb-sdk/src/lib/globals.ts index 186f6d8686..d5ba6d7cde 100644 --- a/packages/nocodb-sdk/src/lib/globals.ts +++ b/packages/nocodb-sdk/src/lib/globals.ts @@ -190,6 +190,7 @@ export enum NcErrorType { UNKNOWN_ERROR = 'UNKNOWN_ERROR', BAD_JSON = 'BAD_JSON', INVALID_PK_VALUE = 'INVALID_PK_VALUE', + COLUMN_ASSOCIATED_WITH_LINK = 'COLUMN_ASSOCIATED_WITH_LINK', } type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles; diff --git a/packages/nocodb/src/helpers/catchError.ts b/packages/nocodb/src/helpers/catchError.ts index 2994434409..8b2d683e11 100644 --- a/packages/nocodb/src/helpers/catchError.ts +++ b/packages/nocodb/src/helpers/catchError.ts @@ -558,6 +558,10 @@ const errorHelpers: { message: 'Invalid JSON in request body', code: 400, }, + [NcErrorType.COLUMN_ASSOCIATED_WITH_LINK]: { + message: 'Column is associated with a link, please remove the link first', + code: 400, + }, }; function generateError( @@ -630,6 +634,9 @@ export class NcError { ...args, }); } + static columnAssociatedWithLink(id: string) { + throw new NcBaseErrorv2(NcErrorType.COLUMN_ASSOCIATED_WITH_LINK); + } static baseNotFound(id: string, args?: NcErrorArgs) { throw new NcBaseErrorv2(NcErrorType.BASE_NOT_FOUND, { diff --git a/packages/nocodb/src/models/Column.ts b/packages/nocodb/src/models/Column.ts index 56bf2212d7..1d4a1e02f1 100644 --- a/packages/nocodb/src/models/Column.ts +++ b/packages/nocodb/src/models/Column.ts @@ -1,6 +1,7 @@ import { AllowedColumnTypesForQrAndBarcodes, isLinksOrLTAR, + isVirtualCol, UITypes, } from 'nocodb-sdk'; import { Logger } from '@nestjs/common'; @@ -674,6 +675,28 @@ export default class Column implements ColumnType { return; } + // 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 (links.length) { + NcError.columnAssociatedWithLink(id); + } + } + // todo: or instead of delete reset related foreign key value to null and handle in BaseModel // get qr code columns and delete