Hello everyone!
I have a Test Chat App where I am trying to fetch chat list using this solution
let latestMessageRequest = Message
.annotated(with: max(Column("date")))
.group(Column("chatID"))
But I also need to fetch latest message's owner, so I just add this:
.including(required: Message.user.forKey("owner"))
struct MessageInfo: Decodable, FetchableRecord {
var message: Message
var owner: User
}
struct ChatInfo: Decodable, FetchableRecord {
var chat: Chat
var latestMessage: Message?
}
let latestMessageRequest = Message
.including(required: Message.user.forKey("owner"))
.annotated(with: max(Column("date")))
.group(Column("chatID"))
Then I get this error:
GRDB/FetchableRecord+Decodable.swift:7: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "owner", intValue: nil), Swift.DecodingError.Context(codingPath: , debugDescription: "No such key: message or owner", underlyingError: nil))
I suppose I don't understand how CTE works, do I? Could anyone help me?
UPDATED:
I found this solution
let messageOwnerAssociation = latestMessageCTE.association(to: User.self) { message, user in
user[Column("id")] == message[Column("userId")]
}
return Chat
.with(latestMessageCTE)
.including(required: latestMessageAssociation.forKey("latestMessage")
.including(required: messageOwnerAssociation.forKey("owner")))
.asRequest(of: ChatInfo.self)
Is it the best way?