question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using count on a bookshelf relation

See original GitHub issue

I 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:open
  • Created 8 years ago
  • Reactions:7
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
mezodcommented, Jul 19, 2017

done!

2reactions
mezodcommented, Jul 19, 2017

I think it’s relevant because it’s a bug that several people have stumbled upon.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found