diff --git a/packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts b/packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts index e387b61688..9c47ab34ac 100644 --- a/packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts +++ b/packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts @@ -2367,39 +2367,26 @@ class KnexClient extends SqlClient { const foreignKeyName = args.foreignKeyName || null; try { - // s = await this.sqlClient.schema.index(Object.keys(args.columns)); - - await this.sqlClient.schema.table(args.childTable, function (table) { - table = table - .foreign(args.childColumn, foreignKeyName) - .references(args.parentColumn) - .on(args.parentTable); - - if (args.onUpdate) { - table = table.onUpdate(args.onUpdate); - } - if (args.onDelete) { - table = table.onDelete(args.onDelete); + const upQb = this.sqlClient.schema.table( + args.childTable, + function (table) { + table = table + .foreign(args.childColumn, foreignKeyName) + .references(args.parentColumn) + .on(args.parentTable); + + if (args.onUpdate) { + table = table.onUpdate(args.onUpdate); + } + if (args.onDelete) { + table.onDelete(args.onDelete); + } } - }); + ); - const upStatement = - this.querySeparator() + - (await this.sqlClient.schema - .table(args.childTable, function (table) { - table = table - .foreign(args.childColumn, foreignKeyName) - .references(args.parentColumn) - .on(args.parentTable); + await upQb; - if (args.onUpdate) { - table = table.onUpdate(args.onUpdate); - } - if (args.onDelete) { - table = table.onDelete(args.onDelete); - } - }) - .toQuery()); + const upStatement = this.querySeparator() + upQb.toQuery(); this.emit(`Success : ${upStatement}`); @@ -2407,7 +2394,7 @@ class KnexClient extends SqlClient { this.querySeparator() + this.sqlClient.schema .table(args.childTable, function (table) { - table = table.dropForeign(args.childColumn, foreignKeyName); + table.dropForeign(args.childColumn, foreignKeyName); }) .toQuery(); diff --git a/packages/nocodb/src/lib/meta/api/columnApis.ts b/packages/nocodb/src/lib/meta/api/columnApis.ts index 0977647a02..36c28b756b 100644 --- a/packages/nocodb/src/lib/meta/api/columnApis.ts +++ b/packages/nocodb/src/lib/meta/api/columnApis.ts @@ -50,6 +50,16 @@ export enum Altered { UPDATE_COLUMN = 8, } +// generate unique foreign key constraint name for foreign key +const generateFkConstraintName = (parent: TableType, child: TableType) => { + const constraintName = `fk_${parent.table_name + .replace(/\W+/g, '_') + .slice(0, 10)}_${child.table_name + .replace(/\W+/g, '_') + .slice(0, 10)}_${randomID(15)}`; + return constraintName; +}; + async function createHmAndBtColumn( child: Model, parent: Model, @@ -316,6 +326,7 @@ export async function columnAdd( onUpdate: 'NO ACTION', type: 'real', parentColumn: parent.primaryKey.column_name, + foreignKeyName: generateFkConstraintName(parent, child), //generateFkConstraintName() }); } @@ -407,6 +418,7 @@ export async function columnAdd( parentTable: parent.table_name, parentColumn: parentPK.column_name, type: 'real', + foreignKeyName: generateFkConstraintName(parent, child), }; const rel2Args = { ...req.body, @@ -415,6 +427,7 @@ export async function columnAdd( parentTable: child.table_name, parentColumn: childPK.column_name, type: 'real', + foreignKeyName: generateFkConstraintName(parent, child), }; await sqlMgr.sqlOpPlus(base, 'relationCreate', rel1Args);