这就是我想要做的(使用PostgreSQL):
选择H.“名称”,A。“名称”
从“比赛” M
加入“团队” H ONM。“ homeTeamID” = H.id
加入“ Teams” A ON M.“ awayTeamID” = A.id
//This will give me an error
return Matches.query(on: request)
.join(\Teams.id, to: \Matches.homeTeamID)
.alsoDecode(Teams.self)
.join(\Teams.id, to: \Matches.awayTeamID)
.alsoDecode(Teams.self)
这是错误:
{
错误:是的,
原因:“表名“ Team”指定了多次”
}
任何帮助表示赞赏!谢谢!
@arema,我尝试重现您的用例,并且Fluent也遇到类似的问题。我在Fluent的github上报告了这个问题:https : //github.com/vapor/fluent/issues/563
这是一种解决方法,但远非优雅。
// Requires conforming `Match` to hashable, Equatable.
func getMatches2Handler(_ req: Request) throws -> Future<[MatchObjects]> {
return map(
to: [MatchObjects].self,
Match.query(on: req).join(\Team.id, to: \Match.homeTeamID).alsoDecode(Team.self).all(),
Match.query(on: req).join(\Team.id, to: \Match.awayTeamID).alsoDecode(Team.self).all()
) { homeTuples, awayTuples in
let homeDictionary = homeTuples.toDictionary()
let awayDictionary = awayTuples.toDictionary()
var matchObjectsArray: [MatchObjects] = []
matchObjectsArray.reserveCapacity(homeDictionary.count)
for (match, homeTeam) in homeDictionary {
let awayTeam = awayDictionary[match]!
matchObjectsArray.append(MatchObjects(match: match, homeTeam: homeTeam, awayTeam: awayTeam))
}
return matchObjectsArray
}
}
//...
extension Array {
func toDictionary<K,V>() -> [K:V] where Iterator.Element == (K,V) {
return self.reduce([:]) {
var dict:[K:V] = $0
dict[$1.0] = $1.1
return dict
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句