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.

Hook not being called from association destroy

See original GitHub issue

I’m using the following calls to generate assoications:

models.Post.hasMany(models.Photo, {as: 'photos', foreignKey: 'post_id', onDelete: 'cascade', hooks: true});
models.Photo.belongsTo(models.Post, {as: 'post', foreignKey: 'post_id', onDelete: 'cascade', hooks: true});

and I have the following hook set up in the Photo model definition:

return sequelize.define('Photo', {
    id: {
      type: 'UUID',
      primaryKey: true,
      allowNull: false
    },
    post_id: {
      type: 'UUID',
      allowNull: false,
    }
  },
  {
    hooks:{
      afterDestroy: function(photo, options) {
        utils.deletePhotos([photo.id]);
      }
    },
    tableName: 'photos'
  });

However when I call:

models.Post.destroy({where: {id: req.params.id}, individualHooks: true});

the photo rows are deleted in the database but the hook is not being called.

Any idea why this might be happening?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
waltonseymourcommented, Apr 14, 2015

I looked in the source and realized that the cascading effect is only triggered through the instance destroy, not the model destroy.

After switching it, it works.

The docs might want to be more clear regarding that though.

0reactions
SimoneLeoni1987commented, Apr 15, 2020

individualHooks http://docs.sequelizejs.com/en/latest/api/model/#destroyoptions-promiseinteger

I didn’t get it… the first question utilizes that boolean value, unsuccesfully… then the user Flexelektro says it’s working… i don’t get it…

To me, the individual hooks value doesn’t work, never did.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ruby on rails - how to hook for destroy of a model that belongs ...
I want to hook for destroy moment at the Experiment model after the owner User get destroyed. has_many :experiments, :dependent => :destroy.
Read more >
Hooks - Sequelize
If your association is Many-to-Many, you may be interested in firing hooks on the through model when using the remove call. Internally, sequelize...
Read more >
hooks · Sequelize-docs
Hooks (also known as callbacks or lifecycle events), are functions which are ... The only way to call beforeDestroy/afterDestroy hooks are on associations...
Read more >
TIL #4: ActiveRecord Dependent Hooks, Callbacks, Execution ...
And since `dependent: :destroy` creates a perfectly normal callback, ActiveRecord tried to destroy an InvoiceTemplate, while still being ...
Read more >
ActiveRecord::Associations::ClassMethods - Rails API
Use has_and_belongs_to_many when working with legacy schemas or when you never ... Assigning an object to a belongs_to association does not save the...
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