Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Sequelize won't update JSON field under some circumstances.

For example, I have:

[[1]] (an array inside array)

And I'm trying push something:

instance.arr[0].push(1); // [[1,1]]
instance.save();
// or:
instance.update({arr: instance.arr});

Now inside instance I have changed array and nothing changed inside db. Not even a query is sent. :(

From sequelize website:

https://sequelize.org/master/manual/model-instances.html The save method is optimized internally to only update fields that really changed. This means that if you don't change anything and call save, Sequelize will know that the save is superfluous and do nothing, i.e., no query will be generated (it will still return a Promise, but it will resolve immediately).

That's good but seems like it doesn't work for json, can I do a force update?

As of today I have to do a deep copy of array to save it.

I'm using MariaDB IDK if that matters.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.7k views
Welcome To Ask or Share your Answers For Others

1 Answer

It seems you have to specify that the field has changed

instance.changed( 'arr', true);
instance.save

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...