I am trying to write a route that returns all the posts written by users who are followed by a particular user. This is the data model that I'm using:
struct Follow: Content, SQLiteModel, Migration {
var id: Int?
var follower: String
var following: String
}
struct Post: Content, SQLiteModel, Migration {
var id: Int?
var username: String
var message: String
}
struct User: Content, SQLiteStringModel, Migration {
var id: String?
var password: String
}
And this is the route:
router.get(String.parameter, "timeline") { req -> Future<[Post]> in
let username = try req.parameters.next(String.self)
return Follow.query(on: req).filter(\Follow.follower == username).join(\Follow.following, to: \Post.username).alsoDecode(Post.self).all().map { tuples in
return tuples.map { tuple in
return tuple.1
}
}
}
The code compiles, but at runtime I get this JSON error dictionary:
{"error":true,"reason":"ambiguous column name: main.Follow.id"}
And I also have an idea that the problem is that since I am making a join, then there is a duplicate id field (Follow.id, Post.id), but how to solve this problem? in sql I would just specify something like 'as followId' in order to rename the field, but how to do this in FluentSQLite?
Update
This is how I modified the "timeline" route in the end:
return User.find(username, on: req).flatMap { user in
guard let user = user else {
throw Abort(.notFound)
}
return try user.followers.query(on: req)
.join(\Post.username, to:\FollowUp.following)
.alsoDecode(Post.self).all()
.flatMap { tuples in
return tuples.map { tuple in
return tuple.1
}
}
}
I get this error: "Cannot convert value of type '[Post]' to closure result type 'EventLoopFuture<[Post]>'"