knexjs列特定类型

时间:2015-06-10 12:54:44

标签: node.js bookshelf.js knex.js

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

我有 用户

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"
  }
}

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

但是 id_sectors 应该是 int type ,有谁知道为什么?

感谢您帮助我。

1 个答案:

答案 0 :(得分:1)

您确定 new Model({id: '1'}).load([relations...])保存为整数吗?

来自文档:

  

例如Model({id: 1}).load([relations...])将不会返回与id_sectors相同的内容 - 请注意,id在一种情况下是字符串,在另一种情况下是数字。如果从url参数中检索id,这可能是一个常见的错误。

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

此外,如果这不起作用,您可以始终使用 long total =0; for(int i=0;i < temp.size(); i++){ if(temp.get(i) > 40){ count++; total+=temp.get(i); } } double mean=0; if(count!=0) mean = total/count; 将字符串值更改为整数。

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