Browse Source

wip: revise m2m delete logic

pull/1958/head
Wing-Kam Wong 3 years ago
parent
commit
a023333e98
  1. 35
      packages/nocodb/src/lib/noco/meta/api/columnApis.ts

35
packages/nocodb/src/lib/noco/meta/api/columnApis.ts

@ -725,7 +725,6 @@ export async function columnDelete(req: Request, res: Response<TableType>) {
},
true
);
const columnsInRelatedTable: Column[] = await relationColOpt
.getRelatedTable()
.then(m => m.getColumns());
@ -737,9 +736,9 @@ export async function columnDelete(req: Request, res: Response<TableType>) {
LinkToAnotherRecordColumn
>();
if (
colOpt.type === 'mm' &&
colOpt.fk_parent_column_id === childColumn.id &&
colOpt.fk_child_column_id === parentColumn.id &&
colOpt.type === 'mm' &&
colOpt.fk_mm_model_id === mmTable.id &&
colOpt.fk_mm_parent_column_id === mmChildCol.id &&
colOpt.fk_mm_child_column_id === mmParentCol.id
@ -752,11 +751,37 @@ export async function columnDelete(req: Request, res: Response<TableType>) {
await Column.delete(relationColOpt.fk_column_id);
await Column.delete(columnInRelatedTable.id);
// delete bt columns in m2m table
await mmTable.getColumns();
for (const c of mmTable.columns) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
const colOpt = await c.getColOptions<
LinkToAnotherRecordColumn
>();
if (colOpt.type === 'bt') {
await Column.delete(c.id);
}
}
// delete hm columns in parent table
await parentTable.getColumns();
for (const c of parentTable.columns) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
const colOpt = await c.getColOptions<
LinkToAnotherRecordColumn
>();
if (colOpt.fk_related_model_id === mmTable.id) {
await Column.delete(c.id);
}
}
// retrieve columns in m2m table again
await mmTable.getColumns();
// ignore deleting table if it have more than 2 columns
// ignore deleting table if it has more than 2 columns
// the expected 2 columns would be table1_id & table2_id
if (mmTable.columns.length === 2) {
await sqlMgr.sqlOpPlus(base, 'tableDelete', mmTable);
await mmTable.delete();
}
}
break;
@ -878,13 +903,13 @@ const deleteHmOrBtRelation = async (
colOpt.type === relType
) {
columnInRelatedTable = c;
await Column.delete(columnInRelatedTable.id, ncMeta);
break;
}
}
// delete virtual columns
await Column.delete(relationColOpt.fk_column_id, ncMeta);
await Column.delete(columnInRelatedTable.id, ncMeta);
if (!ignoreFkDelete) {
const cTable = await Model.getWithInfo({

Loading…
Cancel
Save