From 6bd05b0aa24357db7698b24e96546193d50dfe3e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 13 Feb 2023 15:25:42 +0530 Subject: [PATCH] fix: keep foreign key name in LTAR table Signed-off-by: Pranav C --- packages/nc-gui/assets/style.scss | 2 +- packages/nocodb/src/lib/meta/api/columnApis.ts | 16 ++++++++-------- packages/nocodb/src/lib/meta/api/metaDiffApis.ts | 5 +++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/nc-gui/assets/style.scss b/packages/nc-gui/assets/style.scss index f669139043..50f089d65e 100644 --- a/packages/nc-gui/assets/style.scss +++ b/packages/nc-gui/assets/style.scss @@ -308,7 +308,7 @@ a { .ant-btn-loading-icon{ & > span { - @apply block bg-red-500 + @apply block; } } diff --git a/packages/nocodb/src/lib/meta/api/columnApis.ts b/packages/nocodb/src/lib/meta/api/columnApis.ts index 6fc3af6654..e5ab5dfe8d 100644 --- a/packages/nocodb/src/lib/meta/api/columnApis.ts +++ b/packages/nocodb/src/lib/meta/api/columnApis.ts @@ -51,7 +51,7 @@ export enum Altered { } // generate unique foreign key constraint name for foreign key -const generateFkConstraintName = (parent: TableType, child: TableType) => { +const generateFkName = (parent: TableType, child: TableType) => { // generate a unique constraint name by taking first 10 chars of parent and child table name (by replacing all non word chars with _) // and appending a random string of 15 chars maximum length. // In database constraint name can be upto 64 chars and here we are generating a name of maximum 40 chars @@ -93,7 +93,7 @@ async function createHmAndBtColumn( fk_related_model_id: parent.id, virtual, system: isSystemCol, - fk_col_name: fkColName, + fk_index_name: fkColName, }); } // save hm column @@ -112,7 +112,7 @@ async function createHmAndBtColumn( fk_related_model_id: child.id, virtual, system: isSystemCol, - fk_col_name: fkColName, + fk_index_name: fkColName, }); } } @@ -278,6 +278,7 @@ export async function columnAdd( `${parent.table_name}_id` ); + let foreignKeyName; { // create foreign key const newColumn = { @@ -321,10 +322,9 @@ export async function columnAdd( childColumn = await Column.get({ colId: id }); - let foreignKeyName; // ignore relation creation if virtual if (!(req.body as LinkToAnotherColumnReqType).virtual) { - foreignKeyName = generateFkConstraintName(parent, child); + foreignKeyName = generateFkName(parent, child); // create relation await sqlMgr.sqlOpPlus(base, 'relationCreate', { childColumn: fkColName, @@ -357,7 +357,7 @@ export async function columnAdd( childColumn, (req.body as LinkToAnotherColumnReqType).type as RelationTypes, (req.body as LinkToAnotherColumnReqType).title, - fkColName, + foreignKeyName, (req.body as LinkToAnotherColumnReqType).virtual ); } else if ((req.body as LinkToAnotherColumnReqType).type === 'mm') { @@ -423,8 +423,8 @@ export async function columnAdd( let foreignKeyName2; if (!(req.body as LinkToAnotherColumnReqType).virtual) { - foreignKeyName1 = generateFkConstraintName(parent, child); - foreignKeyName2 = generateFkConstraintName(parent, child); + foreignKeyName1 = generateFkName(parent, child); + foreignKeyName2 = generateFkName(parent, child); const rel1Args = { ...req.body, diff --git a/packages/nocodb/src/lib/meta/api/metaDiffApis.ts b/packages/nocodb/src/lib/meta/api/metaDiffApis.ts index 1708e85717..e9a0fdff93 100644 --- a/packages/nocodb/src/lib/meta/api/metaDiffApis.ts +++ b/packages/nocodb/src/lib/meta/api/metaDiffApis.ts @@ -107,6 +107,7 @@ type MetaDiffChange = { cn?: string; rcn?: string; relationType: RelationTypes; + cstn?: string; } ); @@ -146,6 +147,7 @@ async function getMetaDiff( cn: string; rcn: string; found?: any; + cstn?: string; }> = (await sqlClient.relationListAll())?.data?.list; for (const table of tableList) { @@ -394,6 +396,7 @@ async function getMetaDiff( rcn: relation.rcn, msg: `New relation added`, relationType: RelationTypes.BELONGS_TO, + cstn: relation.cstn, }); } if (!relation?.found?.[RelationTypes.HAS_MANY]) { @@ -736,6 +739,7 @@ export async function metaDiffSync(req, res) { fk_parent_column_id: parentCol.id, fk_child_column_id: childCol.id, virtual: false, + fk_index_name: change.cstn, }); } else if (change.relationType === RelationTypes.HAS_MANY) { const title = getUniqueColumnAliasName( @@ -751,6 +755,7 @@ export async function metaDiffSync(req, res) { fk_parent_column_id: parentCol.id, fk_child_column_id: childCol.id, virtual: false, + fk_index_name: change.cstn, }); } });