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;
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();

13
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);

Loading…
Cancel
Save