Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.2k 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 Reply

0 votes
by (71.8m points)

It seems you have to specify that the field has changed

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...