Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: Force update for a JSON array

Sequelize won't update a JSON field under some circumstances.

For example, I have:

[[1]] (an array inside array)

And I'm trying to push something:

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

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

From the 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 it 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 the array to save it.

I'm using MariaDB. I don't know if that matters.

like image 545
r32 Avatar asked May 14 '26 22:05

r32


1 Answers

It seems you have to specify that the field has changed

instance.changed( 'arr', true);
instance.save
like image 50
Daphoque Avatar answered May 16 '26 12:05

Daphoque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!