Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update instance attribute in "beforeUpdate" hook #10332

Open
oktapodia opened this issue Jan 7, 2019 · 10 comments
Open

Update instance attribute in "beforeUpdate" hook #10332

oktapodia opened this issue Jan 7, 2019 · 10 comments
Labels
topic: hooks For issues and PRs. Things involving hooks. type: bug

Comments

@oktapodia
Copy link
Contributor

oktapodia commented Jan 7, 2019

Hello,

The beforeUpdate hook on single update (not related to individualHook) does not update the query

Link to #5821 but on single update (with .save())

What are you doing?

      beforeUpdate: (instance) => {
        if (instance.changed('plainPassword')) {
          instance.encryptedPassword = hashPassword(instance.plainPassword);

          //instance.setDataValue('encryptedPassword', hashPassword(instance.plainPassword)); Tried with this syntax as well
        }
      },

What do you expect to happen?

Update the SQL query via the beforeUpdate hook

What is actually happening?

The SQL query executed

[0] Executing (default): UPDATE "user" SET "updated_at"='2019-01-07 15:41:27.684 +00:00' WHERE "id" = 'a70a0948-1061-4074-a7fb-f46fd974bb6d'

Dialect: postgres
Dialect version: 7.7.1
Database version: 11
Sequelize version: 4.41.2
Tested with latest release: Yes

Thanks a lot

@oktapodia
Copy link
Contributor Author

Hum, I've got a beforeValidate hook who's actually updating the instance.encryptedPassword field, commenting it out "resolve" the issue

@knoxcard
Copy link
Contributor

Thanks for posting the solution. Close ticket?

@oktapodia
Copy link
Contributor Author

No it's not, if the field is updated into the beforeValidate hook, the SQL query does not handle the updated field

@sushantdhiman
Copy link
Contributor

Can you try with master @oktapodia ?

@oktapodia
Copy link
Contributor Author

oktapodia commented Jan 15, 2019

Sure,

The latest master release is for the v5 isn't it?

Version master#0f0255eed3d88efb179a47fe7763b7309517428a has been used

It's still the same, and the log output is not resolved anymore ($1, $2 and $3).

Not working behavior

// model.js

//...
    hooks: {
	      beforeValidate: (instance) => {
	        if (instance.changed('plainPassword')) {
	          instance.encryptedPassword = hashPassword(instance.plainPassword);
	        }
	      },
	      beforeUpdate: (instance) => {
	        if (instance.changed('plainPassword')) {
	          instance.encryptedPassword = hashPassword(instance.plainPassword);
	        }
	      },
      },
//...
[0] Executing (default): UPDATE "user" SET "token"=$1,"updated_at"=$2 WHERE "id" = $3

Working behavior

// model.js

//...

    hooks: {
      beforeUpdate: (instance) => {
        if (instance.changed('plainPassword')) {
          instance.encryptedPassword = hashPassword(instance.plainPassword);
        }
      },
//...
[0] Executing (default): UPDATE "user" SET "token"=$1,"updated_at"=$2,"encrypted_password"=$3 WHERE "id" = $4

@sushantdhiman
Copy link
Contributor

So beforeValidate and beforeUpdate are not working together

@oktapodia
Copy link
Contributor Author

Exactly sorry if I wasn't clear enough

@markhealey
Copy link

This is still an issue on v5 — we just encountered it where we're have beforeValidate hook code automatically keeping a date field updated when another field value changes. Sequelize will NOT include the update to our date field if we use beforeValidate, only if we use beforeUpdate 😞

@markhealey
Copy link

Actually to be clear, we have other code in the beforeValidate hook that is working. So perhaps it's just date fields where allowNull is true?

@papb papb added status: awaiting investigation topic: hooks For issues and PRs. Things involving hooks. labels Jan 16, 2020
@cbarlow1993
Copy link

Having similar issues here whereby I only have a beforeUpdate hook and trying to add a field in to update,

ReadyToDispatchTimestamp is set in beforeUpdate hook. At the bottom of the beforeUpdate hook, i've run instance.isChanged() and it returns as [ 'State', 'Note', 'ReadyToDispatchTimestamp' ].

I would expect that ReadyToDispatchTimestamp would also be in the executed SQL statement but I get none.

Executing (default): UPDATE Order SET PaymentType=?,CartTotal=?,State=?,CodiceFiscale=?,CouponCode=?,Note=?,updatedAt=? WHERE Id = ?

I've tried making ReadyToDispatchTimestamp a string field and also a Date field, neither worked.

Lost...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: hooks For issues and PRs. Things involving hooks. type: bug
Projects
None yet
Development

No branches or pull requests

7 participants