knexjs 列特定类型

knexjs column specific type

如何在 knexJS 中指定特定类型的列?

我有 table 用户:

id serial NOT NULL,
id_file_avatar bigint,
id_sectors bigint NOT NULL,
name character varying(50),
email character varying(100)

当我休息时,我得到了:

{  
  "user": {
    "id": 1,
    "id_file_avatar": null,
    "id_sectors": "0",
    "name": "Rodrigo Lopes",
    "email": "rodlps22@gmail.com"
  }
}

我的用户模型

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);
        }
    });

但是id_sectors应该是int类型,谁知道为什么?

谢谢你帮助我。

你确定你真的保存id_sectors为整数吗?

来自文档:

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.

尝试为您的模型使用验证器,并设置 id_sectors 必须是整数:https://github.com/fluxxu/bookshelf-validator

此外,如果这不起作用,您可以随时使用 parseInt 将字符串值更改为整数。

至于用属性类型定义模型,我认为(目前)不可能。