{"action":"opened","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/5449","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/5449/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/5449/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/5449/events","html_url":"https://github.com/sequelize/sequelize/issues/5449","id":134570209,"number":5449,"title":"Property sets in beforeValidate hook don't get persisted when updating row","user":{"login":"guisehn","id":830208,"avatar_url":"https://avatars.githubusercontent.com/u/830208?v=3","gravatar_id":"","url":"https://api.github.com/users/guisehn","html_url":"https://github.com/guisehn","followers_url":"https://api.github.com/users/guisehn/followers","following_url":"https://api.github.com/users/guisehn/following{/other_user}","gists_url":"https://api.github.com/users/guisehn/gists{/gist_id}","starred_url":"https://api.github.com/users/guisehn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/guisehn/subscriptions","organizations_url":"https://api.github.com/users/guisehn/orgs","repos_url":"https://api.github.com/users/guisehn/repos","events_url":"https://api.github.com/users/guisehn/events{/privacy}","received_events_url":"https://api.github.com/users/guisehn/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-02-18T12:35:55Z","updated_at":"2016-02-18T12:35:55Z","closed_at":null,"body":"On 3.19.3 with node.js 5.1.0 and Postgres (didn't test with other dialects), when I set some property inside the `beforeValidate` hook when updating a model, some changes set inside the hook don't get persisted.\r\n\r\nModel definition:\r\n```js\r\nvar User = sequelize.define('User', {\r\n  name: { type: Sequelize.STRING, allowNull: false },\r\n  firstName: { type: Sequelize.STRING, allowNull: false },\r\n  lastName: { type: Sequelize.STRING, allowNull: false }\r\n});\r\n```\r\n\r\nHook definition:\r\n```js\r\n// This will trim user.name and update user.firstName and user.lastName\r\n// based on user.name\r\nUser.hook('beforeValidate', function(user, options) {\r\n  if (user.changed('name')) {\r\n    user.set('name', user.name.trim());\r\n\r\n    var parts = user.name.split(' ');\r\n    user.set('firstName', parts[0]);\r\n    user.set('lastName', parts.length > 1 ? parts.pop() : '');\r\n  }\r\n});\r\n```\r\n\r\nFailing test:\r\n```js\r\nsequelize.sync({ force: true }).then(function() {\r\n  return User.create({\r\n    name: ' First Middle Last '\r\n  })\r\n  .then(function(user) {\r\n    return User.findById(user.id); // 1st reload - make sure we are retrieving the data stored in DB\r\n  })\r\n  .then(function(user) {\r\n    assert.equal(user.name, 'First Middle Last'); // passes\r\n    assert.equal(user.firstName, 'First'); // passes\r\n    assert.equal(user.lastName, 'Last'); // passes\r\n\r\n    return user;\r\n  })\r\n  .then(function(user) {\r\n    return user.update({ name: ' NewFirst Middle1 Middle2 Middle3 NewLast  ' });\r\n  })\r\n  .then(function(user) {\r\n    return User.findById(user.id); // 2nd reload - make sure we are retrieving the data stored in DB\r\n  })\r\n  .then(function(user) {\r\n    assert.equal(user.name, 'NewFirst Middle1 Middle2 Middle3 NewLast'); // passes\r\n    assert.equal(user.firstName, 'NewFirst'); // rejects (user.firstName is still \"First\")\r\n    assert.equal(user.lastName, 'NewLast'); // rejects (user.lastName is still \"Last\")\r\n  });\r\n});\r\n```\r\n\r\nQuery log:\r\n```\r\nExecuting (default): INSERT INTO \"Users\" (\"id\",\"name\",\"firstName\",\"lastName\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,'First Middle Last','First','Last','2016-02-18 12:24:02.898 +00:00','2016-02-18 12:24:02.898 +00:00') RETURNING *;\r\n\r\nExecuting (default): SELECT \"id\", \"name\", \"firstName\", \"lastName\", \"createdAt\", \"updatedAt\" FROM \"Users\" AS \"User\" WHERE \"User\".\"id\" = 1;\r\n\r\nExecuting (default): UPDATE \"Users\" SET \"name\"='NewFirst Middle1 Middle2 Middle3 NewLast',\"updatedAt\"='2016-02-18 12:24:02.921 +00:00' WHERE \"id\" = 1 -- only \"name\" got updated\r\n\r\nExecuting (default): SELECT \"id\", \"name\", \"firstName\", \"lastName\", \"createdAt\", \"updatedAt\" FROM \"Users\" AS \"User\" WHERE \"User\".\"id\" = 1;\r\n```\r\n\r\nSome considerations:\r\n- The `firstName` and `lastName` properties are not persisted only on update (it works with create), but `name` persisted on both;\r\n- If I didn't run the 2nd reload (the line indicated in a comment on the failing test), the test would pass, which means that the model instance on JavaScript would have its values updated, but those changes wouldn't get persisted to the DB server;\r\n- This problem doesn't happen if I had used the hooks `beforeCreate` + `beforeUpdate`;\r\n- This issue also happens if I run the same function under the `afterValidate` hook."}}