Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a null value in Sequelize with omitNull flag?

I have an instance of Sequelize with the flag omitNull to insert de defaultValues defined in the DB if i do not define them in the create method of a model. 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. Like:

SomeModel.create({some_property: null});

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

like image 200
vistur Avatar asked Jul 29 '13 13:07

vistur


2 Answers

When using omitNull = true option there is a way to force sequelize to set a property null.

Just do something like this:

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

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

I hope it helps.

like image 102
lao Avatar answered Oct 20 '22 08:10

lao


If you're using sequelize < v2.0, a hackier, alternative solution would be to

sequelize.options.omitNull = false;

SomeModel.some_property = null;
SomeModel.save();

sequelize.options.omitNull = true;
like image 27
max Avatar answered Oct 20 '22 07:10

max