question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

beforeDestroy and afterDestroy hooks not working

See original GitHub issue

Here is what i tried :


var User = sequelize.define('user', {
  firstName: Sequelize.STRING,
  lastName: Sequelize.STRING
}, {
  hooks: {
    beforeDestroy: function(instance, options, cb) {
      console.log('before destroy');
      return cb();
    },
    afterDestroy: function(instance, options, cb) {
      console.log('after destroy');
      return cb();
    },
    beforeCreate: function(instance, options, cb) {
      console.log('before Create');
      return cb();
    },
    afterCreate: function(instance, options, cb) {
      console.log('after Create');
      return cb();
    }
  }
});


sequelize.sync({
  force: false
}).
then(function() {
  User.create({
    firstName: 'e',
    lastName: 'e'
  }).then(function(user) {
    return User.destroy({
      where: {
        id: user.id
      }
    });
  });
});

In the console : before Create Executing (default): INSERT INTO users (id,firstName,lastName,updatedAt,createdAt) VALUES (DEFAULT,‘e’,‘e’,‘2015-04-29 15:41:46’,‘2015-04-29 15:41:46’); after Create Executing (default): DELETE FROM users WHERE id = 7

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
angelxmorenocommented, Feb 16, 2016

I would like to add this since it could be useful:

To set individualHooks to default to true ( so that you don’t have to go back and manually add the params on every call to Model.destroy() )

beforeBulkDestroy: function(options){
    options.individualHooks = true;
    return options;
}
14reactions
janmeiercommented, Apr 29, 2015

Before and after destroy is invoked when destroying a single instance

Use before and after bulkDestroy instead or pass individualHooks: true to destroy

Read more comments on GitHub >

github_iconTop Results From Across the Web

beforeBulkDestroy not finding model property to change
One way you could do this is defining a before/after Destroy hook: hooks: { beforeDestroy: (user, { transaction }) => { user.update({ ...
Read more >
Hooks - Sequelize
Hooks (also known as lifecycle events), are functions which are called before and after calls in sequelize are executed. For example, if you...
Read more >
hooks · Sequelize-docs
Hooks (also known as callbacks or lifecycle events), are functions which are called before and after calls in sequelize are executed. For example,...
Read more >
Sequelize hooks explained with code examples
Learn how Sequelize hooks can be useful for manipulating your database. ... options) beforeDestroy(instance, options) beforeUpdate(instance, ...
Read more >
DS#destroyAll - js-data
Override the default beforeDestroy hook. options.afterDestroy, function, Override the default afterDestroy hook. options.eagerEject, function, Whether to eject ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found