I have tried to use
type: "DATETIME"
defaultsTo: new Date(), but it failed.
Is there an elegant way to set default value?
+1
solved the problem!!
defaultsTo can be a func, the following is my coffeescript
moment = require("moment")
time:
type: "datetime"
defaultsTo: -> moment().format("YYYY-MM-DD HH:mm:ss")
Nice, I'm already using moment.js :+1:
You should be able to use:
time: {
type: 'datetime',
defaultsTo: function () {
return new Date();
}
}
If you are using MySQL, the sails-mysql adapter doesn't honour the timezone shifts properly so your time field will "creep" if you are not careful.
Another way to do it:
time: {
type: 'datetime',
defaultsTo: Date.now
}
The above (Date.now) did not work for me. Waterline was complaining that it was getting an integer instead of a 'datetime'. The following does work though:
time:{
type:"datetime",
defaultsTo: ()=>new Date()
}
Now that functions in defaultsTo are deprecated, what's the right way to do this?
{
my_time: {
type: 'ref',
columnType: 'datetime',
defaultsTo: () => new Date() // Causes an error
}
}
Note that the type 'ref' and columnType 'datetime' are the new way to do mysql datetime fields in Sails 1.0.
Here is the error:
If you need to calculate a value for the attribute before creating a record, try wrapping your
`create` logic in a helper (see http://sailsjs.com/docs/concepts/helpers) or using a lifecycle
hook (see https://sailsjs.com/documentation/concepts/models-and-orm/lifecycle-callbacks#callbacks-on-create).
Are there any new ways to set the current datetime as default value?
That's the current error.
In the `ATTRIB_NAME` attribute of model `MODEL_NAME`:
The `defaultsTo` property can no longer be specified as a function in Sails 1.0. If you
need to calculate a value for the attribute before creating a record, try wrapping your
`create` logic in a helper (see http://sailsjs.com/docs/concepts/helpers)
Most helpful comment
Now that functions in defaultsTo are deprecated, what's the right way to do this?
Note that the type 'ref' and columnType 'datetime' are the new way to do mysql datetime fields in Sails 1.0.