I have two tables "Conversation" and "Message"
Here is the structure of the "conversation" table
| ----- id ---- | ----- createdAt -----|
And you can see the structure of the "message" table below.
| ----- id ---- | ---- conversationId ---- | ---- body ---- | ---- isSeen ----- |
This is my way to fetch each conversation with number of all messages and number of messages where their isSeen
field is equal to false.
struct ConversationInfo: Decodable, FetchableRecord {
var id: Int
var createdAt: Date
var messageCount: Int
var unreadCount: Int
static func request() -> QueryInterfaceRequest<ConversationInfo> {
Conversation
.annotated(with:
Conversation.messages.count.forKey("messageCount"),
Conversation.messages.filter(Message.Columns.isSeen == false).count.forKey("unreadCount")
)
.asRequest(of: ConversationInfo.self)
}
}
But when I run this request I always only get the result of unreadCount
for both fields. For example, If I have 2 messages with field isSeen = false, then messageCount
will be equal to this value. However if I remove Conversation.messages.filter(Message.Columns.isSeen == false).count.forKey("unreadCount")
line from the request, then messageCount
will be received correctly.
struct Conversation: Encodable, PersistableRecord {
var id: Int
var createdAt: Date
static let messages = hasMany(Message.self)
}
struct Message: Encodable, PersistableRecord {
var id: Int
var conversationId: Int
var body: String
var isSeen: Bool
static let conversation = belongsTo(Conversation.self)
}
Thanks for any help