【发布时间】:2021-09-27 21:30:12
【问题描述】:
表结构-
create table test
(month integer,
year integer,
thresholds decimal(18,2)
);
用于模拟的静态插入 -
insert into test(month,year,threshold) values(4,2021,100),(5,2021,98),(6,2021,99);
如果我使用anorm 查询postgres 数据库,它适用于常规查询。但是在添加像 max 这样的聚合函数时,RowParser 无法找到别名列。
val queryString =
"""select max(month) as monthyear from test
| where (month || '-' || year)
| = {inQuery}""".stripMargin
val inQuery1 = "'5-2021'"
下面的on 方法会导致问题 -
val latestInBenchmark = SQL(queryString).on("inQuery" -> inQuery1) // removing the on resolves the problem
logger.info("query for latest period ---> " + latestInBenchmark)
val latestYearMonthInInterval = database.withConnection(implicit conn => {
latestInBenchmark.as(SqlParser.int("monthyear").*)
})
删除on 可以解决问题,SqlParser.int(column-name) 可以按预期工作。
这也不会影响使用 count 聚合函数的查询。
遇到错误:
(Validated intervals with sorting -> ,Failure(anorm.AnormException: 'monthyear' not found, available columns: monthyear, monthyear))
[error] c.b.ThresholdController - 'monthyear' not found, available columns: monthyear, monthyear
【问题讨论】:
-
你永远不应该使用带参数的字符串插值,请先阅读文档
-
也许 OP 在此期间更改了帖子,但这里没有字符串插值。
-
它在没有字符串插值的情况下发生。这是一个复制粘贴错误,因为我使用字符串插值来解决异常参数错误
标签: scala anorm sql-parser