From 06c372d53f186b7e23041b6efe878bffe500c75b Mon Sep 17 00:00:00 2001 From: Higor Tavares <42852956+higortavares@users.noreply.github.com> Date: Wed, 29 Jul 2020 20:39:47 -0300 Subject: [PATCH] fix: foreign key constraint when page have comments (#2199) * Solving foreing key contraint when page have comments * Update pages.js remove indentation changes Co-authored-by: Higor Tavares --- server/models/pages.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/models/pages.js b/server/models/pages.js index 37d0cc11..04ac6f34 100644 --- a/server/models/pages.js +++ b/server/models/pages.js @@ -119,7 +119,15 @@ module.exports = class Page extends Model { this.createdAt = new Date().toISOString() this.updatedAt = new Date().toISOString() } - + /** + * Solving the violates foreign key constraint using cascade strategy + * using static hooks + * @see https://vincit.github.io/objection.js/api/types/#type-statichookarguments + */ + static async beforeDelete({ asFindQuery }) { + const page = await asFindQuery().select('id') + await WIKI.models.comments.query().delete().where('pageId', page[0].id) + } /** * Cache Schema */