I'm trying to run a SQL Raw Query in a Vapor application, and decoded to a MySQLModel but apparently it returns an error.
final class ClassA: MySQLModel {
var id: Int?
var title: String
var description: String
}
But when I run a simple Select From raw query it returns a decoding error: "Value of type 'String' required for key 'title'." with it's weird because the sql run in the MySQL console does run correctly. The thing is when I decode the same query but instead of using a MySQLModel I use a Content like this:
final class ClassB: Content {
var id: Int?
var title: String
var description: String
}
And this is actually ends up decoding without errors.
Edit: The ClassA works perfectly with the regular ORM, it is only when I try to decode from Raw Query that it fails.
Edit: As ask by @Nick the sql does affect the response, when run:
SELECT * FROM ClassA
The return comes back with no error, but my sql includes a complex Subquery like this:
SELECT c.* FROM ClassA c WHERE c.id IN (SELECT id FROM ...);
Note: When run in MySQL Console the query returns no Error and as said the query can be decoded to a Content class exactly like ClassA Example: ClassB.
Edit: The code for running and decoding the raw query is simply:
return request.withNewConnection(to: .mysql) { (con) -> EventLoopFuture<[ClassA]> in
return con.raw(sql).all(decoding: ClassA.self).map { (classes) -> [ClassA] in
return classes
}
}