changed() improperly resets after save() if the value was changed while the INSERT INTO or UPDATE was still running #14510
Labels
type: feature
For issues and PRs. For new features. Never breaking changes.
Issue Creation Checklist
Bug Description
Calling instance.save() will set the changed() status of all fields (if the save() triggered an INSERT INTO) or all changed fields (if the save() triggered an UPDATE) to false. If one of those field values is changed while instance.save() is still running, instance.save() will have save the old value, but will still set changed() to false after running, leaving an instance that is a) different from the database but b) has changed() === false, so it the change will be ignored on subsequent save() calls.
Example:
instance.name = "initial name";
const insertIntoPromise = instance.save();
(without awaiting)await waitUntilNextFrame();
instance.name = "new name";
await insertIntoPromise;
After the save() from step 3 completes we have the following state:
name === "initial name"
name === "new name"
,changed("name") === false
(despite thename
mismatch between instance and database)SSCCE
Here is the link to the SSCCE for this issue: sequelize/sequelize-sscce#228
What do you expect to happen?
Refering to my example above, I'd expect the following state after save() from step 3 completes:
name === "initial name"
name === "new name"
,changed("name") === true
What is actually happening?
After the save() from step 3 completes we have the following state:
name === "initial name"
name === "new name"
,changed("name") === false
(despite thename
mismatch between instance and database)Additional context
Our app consists of a single server that has a lot of objects that are:
Because of these requirements, we opted to keep the whole server state in memory and frequently persist everything into the database via
if (instance.changed()) { instance.save(); }
for every object. That works rather well, but it also means that sometimes a change happens to an instance whileinstance.save()
is running, triggering the bug that is described here and leading to that change never being written to the database.Environment
Would you be willing to resolve this issue by submitting a Pull Request?
I have checked the
save()
method in the sequelize source and I don't think I understand it well enough to properly fix this.The text was updated successfully, but these errors were encountered: