From 408ab7ae1dfa5d1c7dedb2f9fde239596520b2e6 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Fri, 25 May 2018 14:54:00 +0200 Subject: [PATCH] Use cascaded deletes When we delete a user we should delete all the notes that belong to this user including the revisions of these notes. Signed-off-by: Sheogorath --- lib/models/author.js | 8 ++++++-- lib/models/note.js | 4 +++- lib/models/revision.js | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/models/author.js b/lib/models/author.js index 8b4f74e..03f832a 100644 --- a/lib/models/author.js +++ b/lib/models/author.js @@ -24,12 +24,16 @@ module.exports = function (sequelize, DataTypes) { Author.belongsTo(models.Note, { foreignKey: 'noteId', as: 'note', - constraints: false + constraints: false, + onDelete: 'CASCADE', + hooks: true }) Author.belongsTo(models.User, { foreignKey: 'userId', as: 'user', - constraints: false + constraints: false, + onDelete: 'CASCADE', + hooks: true }) } } diff --git a/lib/models/note.js b/lib/models/note.js index 7b9a909..7d8e962 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -91,7 +91,9 @@ module.exports = function (sequelize, DataTypes) { Note.belongsTo(models.User, { foreignKey: 'ownerId', as: 'owner', - constraints: false + constraints: false, + onDelete: 'CASCADE', + hooks: true }) Note.belongsTo(models.User, { foreignKey: 'lastchangeuserId', diff --git a/lib/models/revision.js b/lib/models/revision.js index 9ecd14d..8bc95cb 100644 --- a/lib/models/revision.js +++ b/lib/models/revision.js @@ -102,7 +102,9 @@ module.exports = function (sequelize, DataTypes) { Revision.belongsTo(models.Note, { foreignKey: 'noteId', as: 'note', - constraints: false + constraints: false, + onDelete: 'CASCADE', + hooks: true }) }, getNoteRevisions: function (note, callback) {