Ordering by the COUNT of a relation
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
I’m a bit confused by how to order my data by the COUNT of a relation. If I have a post with likes:
@Entity()
class Post {
@OneToMany('PlaceLike', 'place', { cascade: true, eager: true })
likes: PlaceLike[]
}
How do I ORDER BY the number of Likes a Post has? There seems to be a few undocumented/deprecated ways like @RelationCount
and loadRelationCountAndMap
but they don’t really work. Any ideas?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8
Top Results From Across the Web
Laravel OrderBy relationship count - Stack Overflow
I'm trying to get the most popular hackathons which requires ordering by the respective hackathon's partipants->count() . Sorry if that's a little difficult ......
Read more >Order by relationship count - Laracasts
Order by relationship count ... You can this as a cleaner way to sort by vote count: ... But I don't know what...
Read more >Examples of SQL Order by Count - eduCBA
ORDER BY COUNT clause in standard query language(SQL) is used to sort the result set produced by a SELECT query in an ascending...
Read more >Order By Relationship Count - Laravel - Pine
Let's say we have a Post model and a Comment model. The Comment model has a belongsTo() relationship with the Post, while the...
Read more >Relation field count & ordering by relation count - Database-SQL
Even in v4, there's no count metadata for relation fields. So what's the best approach to be able to easily: get number of...
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
@sekaru just
both has orderBy and Count
wouldn’t this cause querying Comment twice unnecessarily ? one from
addSelect
and one fromload relation
? I felt like it may cause performance issue. Correct me if I’m wrong.Also it seems like there is a terrible bug with typeORM for postgresql
.orderBy('count', 'DESC')
may causecolumn does not exist
, instead useIt’s extremely frustrating after I check the raw query.