Using count on a bookshelf relation
See original GitHub issueI have been trying to do a count like the answer in this SO question: http://stackoverflow.com/questions/32178130/bookshelf-js-relation-getting-count
Doing something like:
* countFollowing(userId) {
return new User({id: userId}).follows().count();
},
doesn’t return the expected number, but the total number of users in the database. On the other hand, and to prove that the follows() relation works, I can do something like this:
* countFollowing(userId) {
const followsJSON = yield this.getFollows(userId);
return Object.keys(followsJSON).length;
},
* getFollows(userId) {
return new User({id: userId})
.fetch({withRelated: 'follows'})
.then(user => {
return user.related('follows').toJSON();
});
},
which returns the expected number.
The definition of the relation is
follows() {
return this.belongsToMany('User', 'Follows', 'follower_id', 'followed_id');
},
Doing something like
* countFollowing(userId) {
/* return new User({id: userId}).follows().count(); */
/* const followsJSON = yield this.getFollows(userId);
return Object.keys(followsJSON).length; */
return yield new User({id: userId})
.fetch()
.then(user => {
return user.follows().count();
});
},
also returns the total amount of users…
is it possible that this doesn’t work for counts because it is a belongsToMany instead of a hasMany, or am I doing something obviously wrong? Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Reactions:7
- Comments:8 (3 by maintainers)
Top Results From Across the Web
node.js - Bookshelf JS Relation - Getting Count - Stack Overflow
The problem with your users_count method is that it tries to make Bookshelf turn the result of your query into Models.
Read more >API Reference - Bookshelf.js
Returns a new instance of the model with identical attributes , including any relations from the cloned model. model.count([column], ...
Read more >Bookshelf.js tutorial - ZetCode
Bookshelf.js tutorial shows how to program databases in ... In the first example, we count the number of rows in the cities table....
Read more >api documentation for bookshelf (v0.10.3)
module bookshelf.relation. function bookshelf.relation.default () ... @param {string} [column='*'] * Specify a column to count - rows with null values in ...
Read more >Count by group in BookshelfJS for SQL (Example) - Coderwall
BookshelfJS is a javascript ORM for Node.js, built on the Knex SQL query builder. If you have complicated query, please use KnexJS, unless...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
done!
I think it’s relevant because it’s a bug that several people have stumbled upon.