1

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?

1
  • I see that the array contain id. It's mean that you only want update value. Right? Mar 5, 2019 at 3:12

1 Answer 1

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

3
  • In document option updateOnDuplicate is Array. Pls update your answer Mar 5, 2019 at 3:32
  • @ChuongTran thank you for noticing. I've updated the answer Mar 5, 2019 at 3:48
  • 1
    Link to reference is wrong, bulkCreate: sequelize.org/api/v6/class/src/…
    – Puce
    Apr 26, 2022 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.