I want to decode the result of query to my model that I used it in my remote request but after join i cant decode with codingKeys because sqlite.swift replaces the double-cotation and it can't find the column name
my code to get data from sqlite table:
let questions = Table("questions")
let replies = Table("replies")
let id = Expression<Int64>("id")
let questionId = Expression<Int64>("question_id")
let query: QueryType = questions.join(replies, on: questions[id] == replies[questionId])
let rows = try! db.prepare(query)
let values: [MyModel] = try rows.map({ try $0.decode() })
MyModel:
struct MyModel: Decodable, Identifiable {
var id: Int
var questionText: String
var replyText: String
enum CodingKeys: String, CodingKey {
case id = "questions.id"
case questionText = "questions.text"
case replyText = "replies.text"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try! container.decode(Int.self, forKey: .id)
self.questionText = try! container.decode(String.self, forKey: .questionText)
self.replyText = try! container.decode(String.self, forKey: .replyText)
}
}
But I faced with this error:
Fatal error: 'try!' expression unexpectedly raised an error: No such column `"questions.id"` in columns [ "\"questions\".\"id\"", "\"questions\".\"text\"", "\"replies\".\"text\"", "\"replies\".\"question_id\""]