sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.62k stars 4.28k forks source link

afterUpdate hook isn't triggered when options.raw = true #7699

Closed Celadora closed 7 years ago

Celadora commented 7 years ago

What you are doing?

updating an instance with option.raw = true

instance.update(values, {raw: true}).then(...)

afterUpdate(instance, options, done) {
        console.log('moo');
        done(null, true);
}

What do you expect to happen?

I expect the afterUpdate hook to be triggered.

What is actually happening?

The hook isn't being triggered

Celadora commented 7 years ago

It turns out that afterUpdate is not triggered if the model is unchanged - but that is just something I discovered when fumbling about while trying to solve this problem.

I've retested with new data for each update (ensuring the model is in fact updated), and if options.raw = true then afterUpdate is still not triggered.

Perhaps this is because options.raw: true doesn't create an instance that afterUpdate can use, but I'm using options.raw: true so that I can pass along values that will be altered before the update ultimately happens. However there should be an instance available since the update method is a prototype on the instance.

Celadora commented 7 years ago

I'm closing this as I've found a better solution. I should have been using a virtual setter for my particular use case. http://docs.sequelizejs.com/manual/tutorial/models-definition.html#getters-setters