From 952dbf6681dba289fbfe7ae266a7bc98e85fdd16 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 22 Jan 2024 07:26:27 +0000 Subject: [PATCH 1/6] fix: block deletion of mm table --- packages/nocodb/src/services/tables.service.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/nocodb/src/services/tables.service.ts b/packages/nocodb/src/services/tables.service.ts index 40e7e190ef..7c6a6b9354 100644 --- a/packages/nocodb/src/services/tables.service.ts +++ b/packages/nocodb/src/services/tables.service.ts @@ -172,6 +172,12 @@ export class TablesService { const table = await Model.getByIdOrName({ id: param.tableId }); await table.getColumns(); + if (table.mm) { + NcError.badRequest( + 'Table is a many to many table. Delete the relation instead.', + ); + } + const base = await Base.getWithInfo(table.base_id); const source = base.sources.find((b) => b.id === table.source_id); From 96400de68e38560d7a20fb14a5c2363a8cd96e5e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 22 Jan 2024 07:26:27 +0000 Subject: [PATCH 2/6] fix: handle if mm table is missing --- packages/nocodb/src/db/BaseModelSqlv2.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 509ce1d58e..ec2abdee45 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -1245,6 +1245,12 @@ class BaseModelSqlv2 { // const tn = this.model.tn; // const cn = (await relColOptions.getChildColumn()).title; const mmTable = await relColOptions.getMMModel(); + + // if mm table is not present then return + if (!mmTable) { + return; + } + const vtn = this.getTnPath(mmTable); const vcn = (await relColOptions.getMMChildColumn()).column_name; const vrcn = (await relColOptions.getMMParentColumn()).column_name; From bd6115eb5eb53d7d1d30a351bbfb234b2adffe8e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 22 Jan 2024 07:26:27 +0000 Subject: [PATCH 3/6] refactor: hide mm relation system columns from ui --- packages/nc-gui/composables/useViewColumns.ts | 4 ++-- packages/nocodb-sdk/src/lib/UITypes.ts | 8 ++++++++ packages/nocodb-sdk/src/lib/index.ts | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/nc-gui/composables/useViewColumns.ts b/packages/nc-gui/composables/useViewColumns.ts index e471dec42a..4f783793c2 100644 --- a/packages/nc-gui/composables/useViewColumns.ts +++ b/packages/nc-gui/composables/useViewColumns.ts @@ -1,4 +1,4 @@ -import { ViewTypes, isCreatedOrLastModifiedByCol, isSystemColumn } from 'nocodb-sdk' +import { ViewTypes, isCreatedOrLastModifiedByCol, isMMSystemCol, isSystemColumn } from 'nocodb-sdk' import type { ColumnType, GridColumnReqType, GridColumnType, MapType, TableType, ViewType } from 'nocodb-sdk' import type { ComputedRef, Ref } from 'vue' import { computed, ref, storeToRefs, useBase, useNuxtApp, useRoles, useUndoRedo, watch } from '#imports' @@ -72,7 +72,7 @@ const [useProvideViewColumns, useViewColumns] = useInjectionState( fields.value = meta.value?.columns ?.filter((column: ColumnType) => { // filter created by and last modified by system columns - if (isCreatedOrLastModifiedByCol(column) && column.system) return false + if ((isCreatedOrLastModifiedByCol(column) || isMMSystemCol(column)) && column.system) return false return true }) .map((column: ColumnType) => { diff --git a/packages/nocodb-sdk/src/lib/UITypes.ts b/packages/nocodb-sdk/src/lib/UITypes.ts index 659a8e744d..1b4eb4911b 100644 --- a/packages/nocodb-sdk/src/lib/UITypes.ts +++ b/packages/nocodb-sdk/src/lib/UITypes.ts @@ -162,6 +162,14 @@ export function isCreatedOrLastModifiedByCol( ); } +export function isMMSystemCol( + col: (ColumnReqType | ColumnType) & { system?: number | boolean } +) { + return ( + col.system && [UITypes.LinkToAnotherRecord].includes(col.uidt) + ); +} + export function isLinksOrLTAR( colOrUidt: ColumnType | { uidt: UITypes | string } | UITypes | string ) { diff --git a/packages/nocodb-sdk/src/lib/index.ts b/packages/nocodb-sdk/src/lib/index.ts index 1e97bd049f..3b674c6791 100644 --- a/packages/nocodb-sdk/src/lib/index.ts +++ b/packages/nocodb-sdk/src/lib/index.ts @@ -16,6 +16,7 @@ export { isLinksOrLTAR, isCreatedOrLastModifiedTimeCol, isCreatedOrLastModifiedByCol, + isMMSystemCol } from '~/lib/UITypes'; export { default as CustomAPI, FileType } from '~/lib/CustomAPI'; export { default as TemplateGenerator } from '~/lib/TemplateGenerator'; From 04e6529ce3bb892780da3c3268f75bc37d8fdbdb Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 22 Jan 2024 07:26:27 +0000 Subject: [PATCH 4/6] refactor: if missing mm table handle it by static error message --- packages/nocodb-sdk/src/lib/globals.ts | 4 ++++ packages/nocodb-sdk/src/lib/index.ts | 2 +- packages/nocodb/src/db/genRollupSelectv2.ts | 8 +++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/globals.ts b/packages/nocodb-sdk/src/lib/globals.ts index b01e8aef82..b5bd47fd12 100644 --- a/packages/nocodb-sdk/src/lib/globals.ts +++ b/packages/nocodb-sdk/src/lib/globals.ts @@ -123,6 +123,10 @@ export enum TiptapMarksTypes { underline = 'underline', } +export enum NcDataErrorCodes { + NC_ERR_MM_MODEL_NOT_FOUND = 'NC_ERR_MM_MODEL_NOT_FOUND', +} + type Roles = OrgUserRoles | ProjectRoles | WorkspaceUserRoles; type RolesObj = Partial>; diff --git a/packages/nocodb-sdk/src/lib/index.ts b/packages/nocodb-sdk/src/lib/index.ts index 3b674c6791..035183e037 100644 --- a/packages/nocodb-sdk/src/lib/index.ts +++ b/packages/nocodb-sdk/src/lib/index.ts @@ -16,7 +16,7 @@ export { isLinksOrLTAR, isCreatedOrLastModifiedTimeCol, isCreatedOrLastModifiedByCol, - isMMSystemCol + isMMSystemCol, } from '~/lib/UITypes'; export { default as CustomAPI, FileType } from '~/lib/CustomAPI'; export { default as TemplateGenerator } from '~/lib/TemplateGenerator'; diff --git a/packages/nocodb/src/db/genRollupSelectv2.ts b/packages/nocodb/src/db/genRollupSelectv2.ts index 05f55ae2f0..cd535c5e13 100644 --- a/packages/nocodb/src/db/genRollupSelectv2.ts +++ b/packages/nocodb/src/db/genRollupSelectv2.ts @@ -1,4 +1,4 @@ -import { RelationTypes } from 'nocodb-sdk'; +import { NcDataErrorCodes, RelationTypes } from 'nocodb-sdk'; import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2'; import type { LinksColumn } from '~/models'; import type { RollupColumn } from '~/models'; @@ -56,6 +56,12 @@ export default async function ({ const mmChildCol = await relationColumnOption.getMMChildColumn(); const mmParentCol = await relationColumnOption.getMMParentColumn(); + if (!mmModel) { + return this.dbDriver.raw(`?`, [ + NcDataErrorCodes.NC_ERR_MM_MODEL_NOT_FOUND, + ]); + } + return { builder: knex( knex.raw(`?? as ??`, [ From ae332d936b48fa46a4a01b668e976ebf19fd319f Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 22 Jan 2024 07:26:27 +0000 Subject: [PATCH 5/6] refactor: introduce `isHiddenCol` method to replace `isMMSystemCol` and `isCreatedOrLastModifiedByCol` --- .../components/smartsheet/column/FormulaOptions.vue | 4 ++-- .../components/smartsheet/toolbar/CreateGroupBy.vue | 6 +++--- .../components/smartsheet/toolbar/CreateSort.vue | 6 +++--- .../toolbar/FieldListAutoCompleteDropdown.vue | 10 +++++----- packages/nc-gui/composables/useViewColumns.ts | 4 ++-- packages/nocodb-sdk/src/lib/UITypes.ts | 11 +++++++++-- packages/nocodb-sdk/src/lib/index.ts | 2 +- 7 files changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue b/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue index d4e3531d27..89279b59c5 100644 --- a/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue +++ b/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue @@ -5,7 +5,7 @@ import jsep from 'jsep' import { FormulaError, UITypes, - isCreatedOrLastModifiedByCol, + isHiddenCol, jsepCurlyHook, substituteColumnIdWithAliasInFormula, validateFormulaAndExtractTreeWithType, @@ -58,7 +58,7 @@ const supportedColumns = computed( return false } - if (isCreatedOrLastModifiedByCol(col) && col.system) { + if (isHiddenCol(col)) { return false } diff --git a/packages/nc-gui/components/smartsheet/toolbar/CreateGroupBy.vue b/packages/nc-gui/components/smartsheet/toolbar/CreateGroupBy.vue index 82785365f7..b0510048ba 100644 --- a/packages/nc-gui/components/smartsheet/toolbar/CreateGroupBy.vue +++ b/packages/nc-gui/components/smartsheet/toolbar/CreateGroupBy.vue @@ -1,6 +1,6 @@