Tuesday, 2 May 2017

How to query by number of related models in Loopback/SQL?

I have a model Post, with a HasMany related model Ratings (also BelongsTo Post).

I want to find all of the Posts with more than 200 Ratings.

So far, the only solutions I can think of are:

  1. Get all of the posts, and manually filter them with a for loop.
  2. Add a column to the Posts table that manually updates as new ratings are added.

(1) is incredibly inefficient (there are thousands of posts), and (2) just doesn't seem right.

I'm using Loopback as a framework and would prefer a built-in solution, but I'm thinking native SQL is the only option here.



via delwin

No comments:

Post a Comment