object array like [{id: 1, a: 1, b: 2}, {id: 2, a: 3, b:4}]
. and I hope to upsert the array to database using sequelize upsert at once. can it support array or should use loop to do it?
Asked
Active
Viewed 966 times
1

dyh333
- 385
- 1
- 5
- 15
-
I see that the array contain id. It's mean that you only want update value. Right? – Chuong Tran Mar 05 '19 at 03:12
1 Answers
1
you can use updateOnDuplicate
along with bulkCreate
method as follows
model.bulkCreate(dataArray, {
updateOnDuplicate: [ 'a', 'b' ] // array of fields to update
}).then(()=> {});
this is only supported by mysql
reference bulkCreate

Aditya Kokil
- 423
- 4
- 11
-
In document option updateOnDuplicate is Array. Pls update your answer – Chuong Tran Mar 05 '19 at 03:32
-
-
1Link to reference is wrong, bulkCreate: https://sequelize.org/api/v6/class/src/model.js~model#static-method-bulkCreate – Puce Apr 26 '22 at 13:36