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);
});

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

This worked for me, thank you.
0

Pass validate true in your bulkCreate method

const createEventAvailability = await EventAvailability.bulkCreate(
    eventAvailability,
    { validate: true }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.