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

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

node.js - Sequelize: Force update for JSON array

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems you have to specify that the field has changed

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...