如何获得行数(没有任何过滤器(?
我已经尝试了此User.fetch().count()
和User.count()
,但是我分别得到这些错误:
TypeError: User.fetch is not a function
AssertionError: expected { Object (_bitField, _fulfillmentHandler0, ...) } to equal 0
这是我的模型:
let bookshelf = require('../config/database');
require('./role');
var User = bookshelf.Model.extend({
tableName: 'users',
role: function() {
return this.hasOne(Role);
}
});
module.exports = bookshelf.model('User', User);
尝试以下:
const knex = require("knex")(require("./knexfile").development);
const Bookshelf = require("bookshelf")(knex);
const User = Bookshelf.Model.extend({
tableName: "user",
idAttribute: "userid"
});
knex.migrate.latest().then(_ => {
User.forge().count().then(ret => {
console.log(ret);
process.exit(0);
});
});
输出应为:
[sombriks@sephiroth sample-knex-bookshelf]$ node index.js
Knex:warning - sqlite does not support inserting default values. Set the `useNullAsDefault` flag to hide this warning. (see docs http://knexjs
.org/#Builder-insert).
3
[sombriks@sephiroth sample-knex-bookshelf]$
如果您想要更详细的信息,请查看此回购。