I'm using Node.js, MySQL and Sequelize. I'd like to insert some 10k rows into a table at once. The table has custom primaryKey
field, that is being set manually. The data are downloaded from web and are overlapping.
I'd like to have a version of bulkCreate
that wouldn't fail if any of the rows in data have unique keys that are already present in the table. Such kind of things is done in MySQL via INSERT ... ON DUPLICATE KEY UPDATE
construct.
How do I do it in Sequelize?
When you need to insert multiple rows to your SQL database table, you can use the Sequelize bulkCreate() method. The bulkCreate() method allows you to insert multiple records to your database table with a single function call.
While Sequelize doesn't provide a bulkUpdate() method, both update() and bulkCreate() methods allow you to update multiple rows with a single method. When you need to update multiple rows with different values, you can use the bulkCreate() method.
A very useful shortcut: the create method Sequelize provides the create method, which combines the build and save methods shown above into a single method: const jane = await User. create({ name: "Jane" }); // Jane exists in the database now!
To add or delete columns in Sequelize CLI, we can use the sequelize migration:create command to create a migration file. Then we call addColumn to add a column and removeColumn to remove a column in the migration file. to create a migration file. in the migration file.
Pass in an options object to bulkCreate with ignoreDuplicates set to true
bulkCreate([...], { ignoreDuplicates: true })
Yuri's answer will ignore duplicates... there is an option for updateOnDuplicate
:
Fields to update if row key already exists (on duplicate key update)? (only supported by mysql & mariadb). By default, all fields are updated.
http://docs.sequelizejs.com/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With