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.

related models count

See original GitHub issue

What is is the desired way to call ‘count’ on a collection?

Say i have a model

User = db.Model.extend({
  tableName: 'users',
  photos: function(){ return this.hasMany(Photo); }
});

new User({id: 1}).fetch().then(function(user) {
  user.photos().count().then(...)  // <- seems to be a good way to query 'select count(*) from photos where user_id = ?'
});

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:36 (22 by maintainers)

github_iconTop GitHub Comments

4reactions
chrisfrancis27commented, Oct 20, 2015

Is there a recommended standard way of achieving this yet? I’ve tried this on my Dataset model:

commentCount: function() {
  return this.hasMany('Comment').query({
    count: 'id as comment_count'
  });
},
comments: function() {
  // One-to-many
  return this.hasMany('Comment');
}

Basically I want a comments array attribute to be populated when retrieving a single model, but a commentCount for each model when retrieving a collection. This seems to generate the right sort of query:

select count("id") as "comment_count" from "comments" where "comments"."dataset_id" in (?, ?, ?)

But the response is returning "commentCount": [] (an empty array, not a number as I’d expect). Any ideas where I’m going wrong?

Cheers

2reactions
frogcjncommented, May 5, 2017
    User.collection().query(function (qb) {
      qb.join('courses', 'users.id', 'courses.user_id');
      qb.groupBy('users.id');
      qb.select("users.*");
      qb.count('* as course_count');
      qb.orderBy("course_count", "desc");
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Django: Count related model where an annotation on the ...
Django: Count related model where an annotation on the related has a specific value and store count in an annotation (or simply: count...
Read more >
Loading Eloquent relationship counts by Tim MacDonald
There are several ways to load relationship counts on eloquent models. I'm going to explore the options and introduce you to a new...
Read more >
Use aggregate methods to count related models
When you're counting related models in @laravelphp, opt for aggregates! Using the count() method on a collection of related models is a tad...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
Counting Related Models ; Other Aggregate Functions; Counting Related Models On Morph To Relationships. Eager Loading. Constraining Eager Loads; Lazy Eager ...
Read more >
QuerySet API reference | Django documentation
that has been computed over the objects that are related to the objects in the ... The Blog model doesn't define an entry__count...
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