how to set default value for a field in Sails models using Waterline ORM?
Asked Answered
S

2

10

A very simple question but I am unable to find the answer to it. So in my Sails app I have a User model and I am trying to make a boolean field with a default value to be False.

Is there a way to specify defaults like some kind of default attribute etc??

I want something like this:

is_admin: {
        type: 'boolean',
        default: 'false'
    }

Thanks for your time.

Superlative answered 3/5, 2014 at 11:29 Comment(0)
D
26

use defaultsTo look this Waterline Docs

Diatropism answered 3/5, 2014 at 15:0 Comment(1)
Be careful! In Mongo the default value is not set on objects older than the field it added a default value. For example if you added a 'locale' field to user model, after you already had some users, the old users will not be added a default locale value. only new users will have it.Illuviation
S
5

To use a contextual example of "defaultsTo" as described in Zieglar's answer

is_admin : {
    type : "boolean",
    defaultsTo : false
}
Simp answered 26/3, 2015 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.