From fb908efe8ff2d435f962152d84ab383bfc323488 Mon Sep 17 00:00:00 2001 From: Rijk van Zanten Date: Wed, 9 Jun 2021 17:02:47 -0400 Subject: [PATCH] Cleanup one_allowed_collections field on collection delete (#6167) Fixes #6161 --- api/src/services/collections.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/api/src/services/collections.ts b/api/src/services/collections.ts index 0c136e6306..6ed9c097df 100644 --- a/api/src/services/collections.ts +++ b/api/src/services/collections.ts @@ -399,6 +399,19 @@ export class CollectionsService { } } + const m2aRelationsThatIncludeThisCollection = this.schema.relations.filter((relation) => { + return relation.meta?.one_allowed_collections?.includes(collectionKey); + }); + + for (const relation of m2aRelationsThatIncludeThisCollection) { + const newAllowedCollections = relation + .meta!.one_allowed_collections!.filter((collection) => collectionKey !== collection) + .join(','); + await trx('directus_relations') + .update({ one_allowed_collections: newAllowedCollections }) + .where({ id: relation.meta!.id }); + } + await collectionItemsService.deleteOne(collectionKey); await trx.schema.dropTable(collectionKey); });