nodejs Sequelize bulkCreate() model error validation not working
Asked Answered
B

3

5

I have multiple data insert operations in my application

and when i use bulkCreate then it works for me

but I have an issue that when some required field like name which is not null in the database and i call create or save method for an individual method without name then it gives the error: required validation error

but when i do the same in bulkCreate then it creates with null value without throwing an error

following is the code of bulkCreate which is not working

models.testModel.bulkCreate(postObj.evUserFavouriteLocations,{updateOnDuplicate: true}).then(function (result) {
    console.log("postObj.");
}).catch(Sequelize.ValidationError, function (err) {
console.log(' in sequierererror', err);
}).catch(function (err) {
console.log("err::",err);
});
Bulgarian answered 25/4, 2018 at 7:12 Comment(0)
B
6

For bulkCreate, validation is turn off by default. You need to pass validate: true in order for validation to work.

Read the document here: http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-bulkCreate

Baum answered 25/4, 2018 at 9:43 Comment(0)
D
2

I fixed it by setting ignoreDuplicates option to true. E.g. Model.bulkCreate(dataArray, {ignoreDuplicates: true})

Dm answered 6/8, 2019 at 8:1 Comment(1)
This worked for me, thank you.Sapsucker
R
0

Pass validate true in your bulkCreate method

const createEventAvailability = await EventAvailability.bulkCreate(
    eventAvailability,
    { validate: true }
);
Rafa answered 14/10 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.