Reputation: 785
How can I specific type of column in knexJS?
I have the table Users:
id serial NOT NULL,
id_file_avatar bigint,
id_sectors bigint NOT NULL,
name character varying(50),
email character varying(100)
When I get in my rest I got it:
{
"user": {
"id": 1,
"id_file_avatar": null,
"id_sectors": "0",
"name": "Rodrigo Lopes",
"email": "[email protected]"
}
}
My UserModel
var User = bookshelf
.Model
.extend({
tableName: 'users',
visible: [
'id',
'id_file_avatar',
'id_sectors',
'name',
'email'
],
soft: false,
initialize: function () {
//this.on('saving', this.validateSave);
},
validateSave: function () {
return new Checkit(rules).run(this.attributes);
}
});
But the id_sectors should be a int type, anyone knows why?
Thank you for helping me.
Upvotes: 0
Views: 1923
Reputation: 3082
Are you sure you actually save id_sectors
as integer?
From the documentation:
For example
new Model({id: '1'}).load([relations...])
will not return the same asModel({id: 1}).load([relations...])
- notice that the id is a string in one case and a number in the other. This can be a common mistake if retrieving the id from a url parameter.
Try using validator for your model, and set that id_sectors
must be integer: https://github.com/fluxxu/bookshelf-validator
Additionally, should this not work, you can always use parseInt
to change your string value to integer.
As for defining a model with attribute types, I don't think that's (currently) possible.
Upvotes: 2