Browse Source

Merge pull request #9733 from nocodb/nc-refactor/bettter-jn-table-name

Nc refactor/bettter jn table name
pull/9734/head
navi 2 months ago committed by GitHub
parent
commit
c16e623e8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 41
      packages/nocodb/src/services/columns.service.ts

41
packages/nocodb/src/services/columns.service.ts

@ -135,6 +135,41 @@ async function reuseOrSave(
return res; return res;
} }
async function getJunctionTableName(
param: {
base: Base;
},
parent: Model,
child: Model,
) {
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() @Injectable()
export class ColumnsService { export class ColumnsService {
constructor( constructor(
@ -3266,7 +3301,7 @@ export class ColumnsService {
param.colExtra, param.colExtra,
); );
} else if ((param.column as LinkToAnotherColumnReqType).type === 'mm') { } else if ((param.column as LinkToAnotherColumnReqType).type === 'mm') {
const aTn = `${param.base?.prefix ?? ''}_nc_m2m_${randomID()}`; const aTn = await getJunctionTableName(param, parent, child);
const aTnAlias = aTn; const aTnAlias = aTn;
const parentPK = parent.primaryKey; const parentPK = parent.primaryKey;
@ -3274,8 +3309,8 @@ export class ColumnsService {
const associateTableCols = []; const associateTableCols = [];
const parentCn = 'table1_id'; const parentCn = `${parent.table_name.slice(0, 30)}_id`;
const childCn = 'table2_id'; const childCn = `${child.table_name.slice(0, 30)}_id`;
associateTableCols.push( associateTableCols.push(
{ {

Loading…
Cancel
Save