我正在编写一个查询来计算 QuestionAnswer 的可能分数,在执行查询时我得到一个 PSQLException
关于模型的信息
QuestionAnswer 可以有多个(至少一个) questionAnswerPossibilities,因为有多种方法可以正确回答问题。
每个 questionAnswerPossibility 都有几个 questionAnswerParts,在下面的查询中,我们查询每个 questionAnswerPossibility 的分数。
有问题的查询
查询本身确实生成了SQL,但是不能执行SQL
def queryMogelijkePuntenByVragenViaOpenVragen()(implicit session: Session) = {
(for{
ovam <- OpenVraagAntwoordMogelijkheden //questionAnswerPossibilites
ovad <- OpenVraagAntwoordOnderdelen if ovad.ovamId === ovam.id //questionAnswerParts
ova <- OpenVraagAntwoorden if ovam.ovaId === ova.id //questionAnswers
} yield ((ova, ovam), ovad.punten))
.groupBy{ case ((ova, ovam), punten) => (ova, ovam)}
.map{ case ((ova, ovam), query) => (ova, ovam, query.map(_._2).sum)}
}
这里生成的SQL(postgreSQL)
select x2."id", x2."vraag_id", x3."id", x3."volgorde", x3."ova_id", sum(x4."punten")
from "open_vraag_antwoord_mogelijkheden" x3, "open_vraag_antwoord_onderdelen" x4, "open_vraag_antwoorden" x2
where (x4."ovam_id" = x3."id") and (x3."ova_id" = x2."id")
group by (x2."id", x2."vraag_id"), (x3."id", x3."volgorde", x3."ova_id")
问题是SQL无法执行,我收到以下错误
play.api.Application$$anon$1:
Execution exception[[
PSQLException: ERROR: column "x2.id" must appear in the GROUP BY clause or be used in an aggregate function
Position: 8]]
生成的 SQL 中括号太多,SQL 的最后一部分应该是
group by x2."id", x2."vraag_id", x3."id", x3."volgorde", x3."ova_id"
但是 slick 用括号生成它,我在这里做错了吗?或者这是一个错误?