From aa5cc48c0ab4b2d04b786d4ce9aed5150ad7a773 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 18 Oct 2023 20:40:46 +0000 Subject: [PATCH] fix: avoid duplicate column_name error by adding suffix --- packages/nocodb/src/helpers/columnHelpers.ts | 7 ++++++- packages/nocodb/src/services/columns.service.ts | 10 ++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/nocodb/src/helpers/columnHelpers.ts b/packages/nocodb/src/helpers/columnHelpers.ts index 9c7f80e6ef..66192845d4 100644 --- a/packages/nocodb/src/helpers/columnHelpers.ts +++ b/packages/nocodb/src/helpers/columnHelpers.ts @@ -264,5 +264,10 @@ export async function populateRollupForLTAR({ } export const sanitizeColumnName = (name: string) => { - return name.replace(/\W+/g, '_').trim(); + const columnName = name.replace(/\W+/g, '_').trim(); + + // if column name only contains _ then return as 'field' + if (/^_+$/.test(columnName)) return 'field'; + + return columnName; }; diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index 5748e17ad9..d08417cb35 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -123,10 +123,7 @@ export class ColumnsService { param.column.column_name = sanitizeColumnName(param.column.column_name); } - if ( - param.column.column_name && - param.column.column_name.length > mxColumnLength - ) { + if (param.column.column_name) { // - 5 is a buffer for suffix let colName = param.column.column_name.slice(0, mxColumnLength - 5); let suffix = 1; @@ -1069,10 +1066,7 @@ export class ColumnsService { param.column.column_name = sanitizeColumnName(param.column.column_name); } - if ( - param.column.column_name && - param.column.column_name.length > mxColumnLength - ) { + if (param.column.column_name) { // - 5 is a buffer for suffix let colName = param.column.column_name.slice(0, mxColumnLength - 5); let suffix = 1;