How to insert null value in Sequelize with omitNull flag?

I have a Sequelize instance with the omitNull flag to insert the default values ​​defined in the database, if I do not define them in the model creation method. Like this:

var sequelize = new Sequelize('db', 'user', 'pw', { omitNull: true }) 

But I want to insert null values ​​in other models if I define them as null. How:

 SomeModel.create({some_property: null}); 

Is there a way to define the omitNull property only for some model properties?

+6
source share
3 answers

When using the omitNull = true option, there is a way to force sseelize to set to null.

Just do something like this:

 SomeModel.some_property = null; SomeModel.save(['some_property']); 

This will force sequelize to create an update / insert SQL file with NULL for some_property .

Hope this helps.

+5
source

If you use sequelize <v2.0, hacking, an alternative solution would be

 sequelize.options.omitNull = false; SomeModel.some_property = null; SomeModel.save(); sequelize.options.omitNull = true; 
+1
source

although this question has been around for a long time, maybe someone needs it ..

  await User.update({ someData: null },{ omitNull: false, where: id, }) 

or

  await User.create({ someData: null },{ omitNull: false, }) 
0
source

Source: https://habr.com/ru/post/950531/


All Articles