简体   繁体   中英

knexjs column specific type

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": "rodlps22@gmail.com"
  }
}

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.

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 as Model({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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM