gpt4 book ai didi

postgresql - 如何使用 Fluent 从 Vapor 3 中的同一个表进行两个 JOIN 查询?

转载 作者:行者123 更新时间:2023-11-29 11:49:02 24 4
gpt4 key购买 nike

这就是我想要做的(使用 postgresql):

选择H."name", A."name"

从“匹配”M

加入“团队”H ON M."homeTeamID"= H.id

加入“团队”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)

这里是错误:

{

错误:正确,

原因:“多次指定表名“Teams””

感谢任何帮助!谢谢!

最佳答案

@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
}
}
}

关于postgresql - 如何使用 Fluent 从 Vapor 3 中的同一个表进行两个 JOIN 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com