Hook not being called from association destroy
See original GitHub issueI’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:
- Created 8 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.