Calling QueryAttributes With Aggregate Count On One-To-Many Relationship #284

Closed
opened 2025-12-29 15:28:11 +01:00 by adam · 2 comments
Owner

Originally created by @damianstrain on GitHub (Aug 1, 2019).

Hi John,

I'm executing the following (where "boards" is a one-to-many relationship on Project):

try CoreStore.queryAttributes(
    From<Project>(),
    Select("id", "key", "name", "createdOn", "updatedOn", .count("boards", as: "boardCount"))
)

Only the following attributes seem to be returned: "id", "key", "name", "createdOn", "updatedOn" and there are no exceptions or errors present.

I know that Project has 2 Boards, so I was expecting "boardCount" to be 2.
Is what I'm doing valid? Can you call .count on a one-to-many relationship?

Many Thanks

Originally created by @damianstrain on GitHub (Aug 1, 2019). Hi John, I'm executing the following (where `"boards"` is a one-to-many relationship on `Project`): ```swift try CoreStore.queryAttributes( From<Project>(), Select("id", "key", "name", "createdOn", "updatedOn", .count("boards", as: "boardCount")) ) ``` Only the following attributes seem to be returned: `"id", "key", "name", "createdOn", "updatedOn"` and there are no exceptions or errors present. I know that Project has 2 Boards, so I was expecting `"boardCount"` to be 2. Is what I'm doing valid? Can you call `.count` on a one-to-many relationship? Many Thanks
adam added the question label 2025-12-29 15:28:11 +01:00
adam closed this issue 2025-12-29 15:28:11 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Sep 22, 2019):

@damianstrain Sorry for the long wait. The count operator here is a collection operator, which means it will count a value for a property if you use the GroupBy clause as well. Instead, can you try

Select("id", "key", "name", "createdOn", "updatedOn", .attribute("boards.@count", as: "boardCount")))
@JohnEstropia commented on GitHub (Sep 22, 2019): @damianstrain Sorry for the long wait. The count operator here is a collection operator, which means it will count a value for a property if you use the `GroupBy` clause as well. Instead, can you try ```swift Select("id", "key", "name", "createdOn", "updatedOn", .attribute("boards.@count", as: "boardCount"))) ```
Author
Owner

@damianstrain commented on GitHub (Sep 25, 2019):

Excellent! Thanks @JohnEstropia

@damianstrain commented on GitHub (Sep 25, 2019): Excellent! Thanks @JohnEstropia
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#284