Sequelize: bulk upsert

Created on 25 May 2016  路  3Comments  路  Source: sequelize/sequelize

How does one generate a query like this on mysql?

INSERT INTO t (t.a, t.b, t.c)
VALUES ('key1','key2','value'), ('key1','key3','value2')
ON DUPLICATE KEY UPDATE
t.c = VALUES(t.c)

On a single row, this is what upsert does, but it does not accept array of values. I have a use case where I need to either update or create multiple rows - I do not care for return values.

Most helpful comment

Solved using bulkCreate with updateOnDuplicate option.
In the documentation it states that by default all fields are updated, but that was not the case - ON DUPLICATE KEY UPDATE was not added. Had to set this property explicitly.

All 3 comments

Solved using bulkCreate with updateOnDuplicate option.
In the documentation it states that by default all fields are updated, but that was not the case - ON DUPLICATE KEY UPDATE was not added. Had to set this property explicitly.

if you set updateOnDuplicate to a boolean value it will simply be ignored, that field should contain an array of fields, the code specifically checks for it to be an array, if not it does nothing

Solved using bulkCreate with updateOnDuplicate option.
In the documentation it states that by default all fields are updated, but that was not the case - ON DUPLICATE KEY UPDATE was not added. Had to set this property explicitly.

updateOnDuplicate : [ ]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haikyuu picture haikyuu  路  3Comments

hassan-ahmed picture hassan-ahmed  路  3Comments

ryanwalters picture ryanwalters  路  3Comments

Bondifrench picture Bondifrench  路  3Comments

lfcgomes picture lfcgomes  路  3Comments