Is there a way to do a 'limit' on a relationship?
My use case is eventually an aggregate, like 'count of the last 10 posts that are unpublished'.
Was thinking I could get something working if I set up a relationship that was 'most recent 10 posts', and then an aggregate count on that relationship, but not seeing anything on how that'd work.
Any ideas?
7 Replies
you can indeed:
oh, wait
I guess you can't 🤔
I really thought you could do that 😓
but you can limit an aggregate FWIW
but apparently only the inline calculation type of aggregate. Wow, how does everyone ask something innocent and stumble into a mess 😜
Something like this should work though
And thank you for being the first person to use the new forum setup ❤️
We could support
limit
on aggregate DSL and on has_many
relationships though FWIW
Feel free to open proposals for both 😄* Cannot set limit on aggregate queryhm i like the idea of doing the limit on a
has_many
relationship, that seems pretty clean
https://github.com/ash-project/ash/blob/f6688fcc71b750f8d96222ec8eb157b0eff523ea/lib/ash/query/aggregate.ex#L523
getting this even if it's an inline calculation type aggregate rather than an aggregate count typehuh. Maybe I'm wrong
i'll make a proposal for limits + sort on a has_many relationship
right-o
you can't do it apparently 😢
You can definitely do
sort
just not limit apparently. I'll have to look into why limit
isn't supported on aggregate querieshttps://github.com/ash-project/ash/issues/2012 - made this
so I think right now my best bet might be to just do things through code, like:
* read action for getting last 10 posts that are unpublished
* Enum.count or similar
Yeah, that would work. You could also see what happens if you do this: