From 7c165511a1fb1866b51831634d7ea694f68e680c Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 27 Oct 2024 08:37:51 +0000 Subject: [PATCH 1/2] feat: use better junction table name and column names --- .../nocodb/src/services/columns.service.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index 4f40fabd56..10dcb764d0 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -135,6 +135,18 @@ async function reuseOrSave( return res; } +function getJunctionTableName( + param: { + base: Base; + }, + parent: Model, + child: Model, +) { + return `${param.base?.prefix ?? ''}_nc_m2m_${parent.table_name}_${ + child.table_name + }`; +} + @Injectable() export class ColumnsService { constructor( @@ -3266,7 +3278,7 @@ export class ColumnsService { param.colExtra, ); } else if ((param.column as LinkToAnotherColumnReqType).type === 'mm') { - const aTn = `${param.base?.prefix ?? ''}_nc_m2m_${randomID()}`; + const aTn = getJunctionTableName(param, parent, child); const aTnAlias = aTn; const parentPK = parent.primaryKey; @@ -3274,8 +3286,8 @@ export class ColumnsService { const associateTableCols = []; - const parentCn = 'table1_id'; - const childCn = 'table2_id'; + const parentCn = `${parent.table_name}_id`; + const childCn = `${child.table_name}_id`; associateTableCols.push( { From a72e59e7c19c08bd762e10f76f5cf3b506bdecef Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 27 Oct 2024 08:37:51 +0000 Subject: [PATCH 2/2] refactor: limit table name length --- .../nocodb/src/services/columns.service.ts | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/services/columns.service.ts b/packages/nocodb/src/services/columns.service.ts index 10dcb764d0..df56c3f9ea 100644 --- a/packages/nocodb/src/services/columns.service.ts +++ b/packages/nocodb/src/services/columns.service.ts @@ -135,16 +135,39 @@ async function reuseOrSave( return res; } -function getJunctionTableName( +async function getJunctionTableName( param: { base: Base; }, parent: Model, child: Model, ) { - return `${param.base?.prefix ?? ''}_nc_m2m_${parent.table_name}_${ - child.table_name - }`; + const parentTable = param.base?.prefix + ? parent.table_name.replace(`${param.base?.prefix}_`, '') + : parent.table_name; + const childTable = param.base?.prefix + ? child.table_name.replace(`${param.base?.prefix}_`, '') + : child.table_name; + + const tableName = `${ + param.base?.prefix ?? '' + }_nc_m2m_${parentTable.slice(0, 15)}_${childTable.slice(0, 15)}`; + let suffix: number = null; + // check table name avail or not, if not then add incremental suffix + while ( + await Noco.ncMeta.metaGet2( + (parent as any).fk_workspace_id, + parent.base_id, + MetaTable.MODELS, + { + table_name: `${tableName}${suffix ?? ''}`, + source_id: parent.source_id, + }, + ) + ) { + suffix = suffix ? suffix + 1 : 1; + } + return `${tableName}${suffix ?? ''}`; } @Injectable() @@ -3278,7 +3301,7 @@ export class ColumnsService { param.colExtra, ); } else if ((param.column as LinkToAnotherColumnReqType).type === 'mm') { - const aTn = getJunctionTableName(param, parent, child); + const aTn = await getJunctionTableName(param, parent, child); const aTnAlias = aTn; const parentPK = parent.primaryKey; @@ -3286,8 +3309,8 @@ export class ColumnsService { const associateTableCols = []; - const parentCn = `${parent.table_name}_id`; - const childCn = `${child.table_name}_id`; + const parentCn = `${parent.table_name.slice(0, 30)}_id`; + const childCn = `${child.table_name.slice(0, 30)}_id`; associateTableCols.push( {