From bd085facaea9901c7f7979baf14e41a52d24ea96 Mon Sep 17 00:00:00 2001 From: mertmit Date: Thu, 5 Jan 2023 15:48:13 +0300 Subject: [PATCH] fix: remove leftover relations from col_relations on model delete Signed-off-by: mertmit --- packages/nocodb/src/lib/models/Base.ts | 2 +- packages/nocodb/src/lib/models/Model.ts | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/lib/models/Base.ts b/packages/nocodb/src/lib/models/Base.ts index 2255ad6571..51afee3866 100644 --- a/packages/nocodb/src/lib/models/Base.ts +++ b/packages/nocodb/src/lib/models/Base.ts @@ -278,7 +278,7 @@ export default class Base implements BaseType { ncMeta ); for (const model of models) { - await model.delete(ncMeta); + await model.delete(ncMeta, true); } await NocoCache.deepDel( CacheScope.BASE, diff --git a/packages/nocodb/src/lib/models/Model.ts b/packages/nocodb/src/lib/models/Model.ts index 6d61e9a048..a8fe688707 100644 --- a/packages/nocodb/src/lib/models/Model.ts +++ b/packages/nocodb/src/lib/models/Model.ts @@ -345,7 +345,7 @@ export default class Model implements TableType { }); } - async delete(ncMeta = Noco.ncMeta): Promise { + async delete(ncMeta = Noco.ncMeta, force = false): Promise { await Audit.deleteRowComments(this.id); for (const view of await this.getViews(true)) { @@ -391,6 +391,26 @@ export default class Model implements TableType { } } + if (force) { + const leftOverColumns = await ncMeta.metaList2(null, null, MetaTable.COL_RELATIONS, { + condition: { + fk_related_model_id: this.id, + }, + }); + + for (const col of leftOverColumns) { + await NocoCache.deepDel( + CacheScope.COL_RELATION, + `${CacheScope.COL_RELATION}:${col.fk_column_id}`, + CacheDelDirection.CHILD_TO_PARENT + ); + } + + await ncMeta.metaDelete(null, null, MetaTable.COL_RELATIONS, { + fk_related_model_id: this.id, + }); + } + await NocoCache.deepDel( CacheScope.COLUMN, `${CacheScope.COLUMN}:${this.id}`,