Browse Source

fix(nocodb): populate unique fk constraint name on relation column create

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5030/head
Pranav C 2 years ago
parent
commit
e84f60d5d6
  1. 49
      packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts
  2. 13
      packages/nocodb/src/lib/meta/api/columnApis.ts

49
packages/nocodb/src/lib/db/sql-client/lib/KnexClient.ts

@ -2367,39 +2367,26 @@ class KnexClient extends SqlClient {
const foreignKeyName = args.foreignKeyName || null; const foreignKeyName = args.foreignKeyName || null;
try { try {
// s = await this.sqlClient.schema.index(Object.keys(args.columns)); const upQb = this.sqlClient.schema.table(
args.childTable,
await this.sqlClient.schema.table(args.childTable, function (table) { function (table) {
table = table table = table
.foreign(args.childColumn, foreignKeyName) .foreign(args.childColumn, foreignKeyName)
.references(args.parentColumn) .references(args.parentColumn)
.on(args.parentTable); .on(args.parentTable);
if (args.onUpdate) { if (args.onUpdate) {
table = table.onUpdate(args.onUpdate); table = table.onUpdate(args.onUpdate);
} }
if (args.onDelete) { if (args.onDelete) {
table = table.onDelete(args.onDelete); table.onDelete(args.onDelete);
}
} }
}); );
const upStatement = await upQb;
this.querySeparator() +
(await this.sqlClient.schema
.table(args.childTable, function (table) {
table = table
.foreign(args.childColumn, foreignKeyName)
.references(args.parentColumn)
.on(args.parentTable);
if (args.onUpdate) { const upStatement = this.querySeparator() + upQb.toQuery();
table = table.onUpdate(args.onUpdate);
}
if (args.onDelete) {
table = table.onDelete(args.onDelete);
}
})
.toQuery());
this.emit(`Success : ${upStatement}`); this.emit(`Success : ${upStatement}`);
@ -2407,7 +2394,7 @@ class KnexClient extends SqlClient {
this.querySeparator() + this.querySeparator() +
this.sqlClient.schema this.sqlClient.schema
.table(args.childTable, function (table) { .table(args.childTable, function (table) {
table = table.dropForeign(args.childColumn, foreignKeyName); table.dropForeign(args.childColumn, foreignKeyName);
}) })
.toQuery(); .toQuery();

13
packages/nocodb/src/lib/meta/api/columnApis.ts

@ -50,6 +50,16 @@ export enum Altered {
UPDATE_COLUMN = 8, 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( async function createHmAndBtColumn(
child: Model, child: Model,
parent: Model, parent: Model,
@ -316,6 +326,7 @@ export async function columnAdd(
onUpdate: 'NO ACTION', onUpdate: 'NO ACTION',
type: 'real', type: 'real',
parentColumn: parent.primaryKey.column_name, parentColumn: parent.primaryKey.column_name,
foreignKeyName: generateFkConstraintName(parent, child), //generateFkConstraintName()
}); });
} }
@ -407,6 +418,7 @@ export async function columnAdd(
parentTable: parent.table_name, parentTable: parent.table_name,
parentColumn: parentPK.column_name, parentColumn: parentPK.column_name,
type: 'real', type: 'real',
foreignKeyName: generateFkConstraintName(parent, child),
}; };
const rel2Args = { const rel2Args = {
...req.body, ...req.body,
@ -415,6 +427,7 @@ export async function columnAdd(
parentTable: child.table_name, parentTable: child.table_name,
parentColumn: childPK.column_name, parentColumn: childPK.column_name,
type: 'real', type: 'real',
foreignKeyName: generateFkConstraintName(parent, child),
}; };
await sqlMgr.sqlOpPlus(base, 'relationCreate', rel1Args); await sqlMgr.sqlOpPlus(base, 'relationCreate', rel1Args);

Loading…
Cancel
Save