diff --git a/packages/nocodb/src/lib/noco/meta/api/columnApis.ts b/packages/nocodb/src/lib/noco/meta/api/columnApis.ts index 3871493181..9f76c97c00 100644 --- a/packages/nocodb/src/lib/noco/meta/api/columnApis.ts +++ b/packages/nocodb/src/lib/noco/meta/api/columnApis.ts @@ -728,7 +728,6 @@ export async function columnDelete(req: Request, res: Response) { const columnsInRelatedTable: Column[] = await relationColOpt .getRelatedTable() .then(m => m.getColumns()); - let columnInRelatedTable: Column; for (const c of columnsInRelatedTable) { if (c.uidt !== UITypes.LinkToAnotherRecord) continue; @@ -743,13 +742,12 @@ export async function columnDelete(req: Request, res: Response) { colOpt.fk_mm_parent_column_id === mmChildCol.id && colOpt.fk_mm_child_column_id === mmParentCol.id ) { - columnInRelatedTable = c; + await Column.delete(c.id); break; } } await Column.delete(relationColOpt.fk_column_id); - await Column.delete(columnInRelatedTable.id); // delete bt columns in m2m table await mmTable.getColumns(); @@ -775,6 +773,18 @@ export async function columnDelete(req: Request, res: Response) { } } + // delete hm columns in child table + await childTable.getColumns(); + for (const c of childTable.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(); @@ -892,7 +902,6 @@ const deleteHmOrBtRelation = async ( const columnsInRelatedTable: Column[] = await relationColOpt .getRelatedTable() .then(m => m.getColumns()); - let columnInRelatedTable: Column; const relType = relationColOpt.type === 'bt' ? 'hm' : 'bt'; for (const c of columnsInRelatedTable) { if (c.uidt !== UITypes.LinkToAnotherRecord) continue; @@ -902,8 +911,7 @@ const deleteHmOrBtRelation = async ( colOpt.fk_child_column_id === childColumn.id && colOpt.type === relType ) { - columnInRelatedTable = c; - await Column.delete(columnInRelatedTable.id, ncMeta); + await Column.delete(c.id, ncMeta); break; } }