How to set an attribute null when using omitNull option

1,364 views
Skip to first unread message

lucas abreu

unread,
Aug 1, 2014, 4:47:26 PM8/1/14
to sequ...@googlegroups.com
I saw someone asking this question on stackoverflow ( this question ). And I was wondering, if anyone have find a way to solve this problem.


Here is the original question:

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?

Mick Hansen

unread,
Aug 2, 2014, 4:15:00 AM8/2/14
to lucas abreu, sequ...@googlegroups.com
No there is not. Honestly i don't think you should really use omitNull: true anymore, it was a hack to solve a specific problem but that problem can be solved in better ways these days.
--
Mick Hansen
@mhansendev
mhansen.io

lucas abreu

unread,
Aug 2, 2014, 10:28:21 AM8/2/14
to sequ...@googlegroups.com, lucasabre...@gmail.com
There is a way.

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 = nulll;
SomeModel.save(['some_property']);

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

I agree omiNull is not the best solution. I hope sequelize team solves this problem soon. =)

lucas abreu

unread,
Aug 2, 2014, 10:29:33 AM8/2/14
to sequ...@googlegroups.com, lucasabre...@gmail.com

Mick Hansen

unread,
Aug 3, 2014, 10:42:15 AM8/3/14
to lucas abreu, sequ...@googlegroups.com
That bug should not exist anymore on the latest master. If a attribute is correctly marked as AI/PK and not initialized with a value (i.e. undefined, null is a value) the query should ignore it. Fix is probably also on stable but i'm not totally certain.
Reply all
Reply to author
Forward
0 new messages