question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

instance.save() doesn't detect and save changes in arrays or objects

See original GitHub issue

Consider 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:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
adamjaffebackcommented, Jan 14, 2016

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; use update instead.

instance.dogs;
// ['lab', 'poodle']

// Wrong
instance.dogs.pop();
instance.dogs;
// ['lab']
instance.save()
.then(function(savedInstance) {
  savedInstance.dogs;
  // ['lab', 'poodle']
  // no change flag, no persistence
});

// Correct Usage
instance.update({ dogs: ['lab'] })
.then(function(updatedInstance) {
  updatedInstance.dogs;
  // ['lab']
  // persistence
});

On success, the callback[…]

0reactions
jcalfeecommented, Oct 21, 2022

I think I figured it out. It looks like Sequelize is doing an equality check ===, so I can clone the array b = [...a || []] or just b = [...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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found