[local-sync, iso-core]: Cascade deletes on hasMany associations

Summary:
Delete associated children when a parent is deleted to prevent foreign
key constraint errors. Also make sure any child hooks are run.

Test Plan: tested locally

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3620
This commit is contained in:
Halla Moore 2017-01-09 18:06:54 -08:00
parent aaf0b04ae3
commit 2ed1a03d34
4 changed files with 4 additions and 4 deletions

View file

@ -28,7 +28,7 @@ module.exports = (sequelize, Sequelize) => {
],
classMethods: {
associate(data = {}) {
Account.hasMany(data.AccountToken, {as: 'tokens'})
Account.hasMany(data.AccountToken, {as: 'tokens', onDelete: 'cascade', hooks: true})
},
upsertWithCredentials(accountParams, credentials) {
if (!accountParams || !credentials || !accountParams.emailAddress) {

View file

@ -40,7 +40,7 @@ module.exports = (sequelize, Sequelize) => {
}, {
classMethods: {
associate({Folder, Message, Thread}) {
Folder.hasMany(Message)
Folder.hasMany(Message, {onDelete: 'cascade', hooks: true})
Folder.belongsToMany(Thread, {through: 'thread_folders'})
},

View file

@ -82,7 +82,7 @@ module.exports = (sequelize, Sequelize) => {
Message.belongsTo(Thread)
Message.belongsTo(Folder)
Message.belongsToMany(Label, {through: MessageLabel})
Message.hasMany(File)
Message.hasMany(File, {onDelete: 'cascade', hooks: true})
},
hash({from = [], to = [], cc = [], bcc = [], date = '', subject = '', headerMessageId = ''} = {}) {

View file

@ -42,7 +42,7 @@ module.exports = (sequelize, Sequelize) => {
associate: ({Thread, Folder, ThreadFolder, Label, ThreadLabel, Message}) => {
Thread.belongsToMany(Folder, {through: ThreadFolder})
Thread.belongsToMany(Label, {through: ThreadLabel})
Thread.hasMany(Message)
Thread.hasMany(Message, {onDelete: 'cascade', hooks: true})
},
},
instanceMethods: {