instance.save() doesn't detect and save changes in arrays or objects
See original GitHub issueConsider following schema
var Model = sequelize.define('model', {
array: Sequelize.ARRAY(Sequelize.INTEGER)
});
and following code
Model.create({
array: [1],
}).then(function(instance) {
instance.array.push(2);
instance.save().then(function() {
instance.reload().then(function() {
console.log(instance.array);
});
});
});
instance.array outputs [1]
, but I expect it to be [1,2]
.
Documentation states that When you call save without changing any attribute, this method will execute nothing
, but I’m guessing only object references are being checked and not the actual values.
Same issue can be reproduced with Sequelize.JSONB
type as well.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:5
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Mongoose instance .save() not working when embedded ...
The problem is that mongoose don't knwo your array is modified. You can use 2 solutions : markModified. This function will mark the...
Read more >Save and Load Process for Objects - MATLAB & Simulink
To save graphics objects, see savefig . How Is the Property Data Loaded? When loading objects from MAT-files, the load function restores the...
Read more >Saving Data | Firebase Realtime Database - Google
Then use set() / setValue() to save a user object to the database with the ... You can pass set a string, number,...
Read more >Data Structures: Objects and Arrays - Eloquent JavaScript
This example demonstrates two methods you can use to manipulate arrays: ... Even though number values don't change, you can use a let...
Read more >Arrays - The Modern JavaScript Tutorial
Objects allow you to store keyed collections of values. ... The pop method does not need to move anything, because other elements keep...
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
This issue had me thinking I was crazy for a good 30 minutes. I’d like to submit a PR to improve the API documentation, but I’m not sure on the wording. Please review a draft:
Original
Validate this instance, and if the validation passes, persist it to the database. It will only save changed fields, and do nothing if no fields have changed.
On success, the callback[…]
Replacement
[…] do nothing if no fields have changed.
Note: If a JSON/JSONB attribute is called with a dot.seperated key, it will flag the entire object as changed. Changes to ARRAY types do not set a flag as changed and will not be persisted with
save
; useupdate
instead.On success, the callback[…]
I think I figured it out. It looks like Sequelize is doing an equality check
===
, so I can clone the arrayb = [...a || []]
or justb = [...a]
then if I change the clone I assign the cloned array back to the model and it saves. There was no need to add additional Sequelize method calls to do this.