当相同的查询在Postgres上运行正常时,H2 SQL语法错误

问题描述

我一直在使用slick进行数据库交互,并使用h2内存数据库进行测试。因此,我刚刚添加了一种新方法,在该方法中,我将普通的SQL编写为查询而不是简单的查询。

平滑版本:3.3.0 HikariCp:3.3.0 H2:1.4.200

因此,它会因带有语法错误异常的普通sql查询而失败。

 def filterTasksByGivenConstraints(taskIds: List[Long],constraints: String): Future[Vector[Int]] = {
          val ids = taskIds.mkString("'","','","'")
          
          println(s"SELECT id FROM task where id in ($ids) and $constraints")

          val query = sql"""SELECT "id" FROM "task" WHERE "id" IN (#$ids) and #$constraints""".as[Int]
           db.get.run(query)
              .recover {
                case ex: Exception =>
                  logger.error(s"An exception has occurred while fetching filtered tasks $ids for given constraints $constraints and exception is: $ex")
                  throw ex.getCause
            
        }
      }

以println打印的查询:

SELECT id FROM task where id in ('1') and ("expire_on" <= '2020-07-18') and (("expire_on" at time zone 'EST')::date >= '2020-07-16'::date and ("expire_on" at time zone 'EST')::date <= '2020-07-18'::date) and (("claim_number" = 'foo20') or ("referred_to" = '21'))

例外:

15:16:01.063 [scala-execution-context-global-23] ERROR io.inbox.stream.db.DBOperations - {"Level":"ERROR","Message":"An exception has occurred while fetching filtered tasks '1' for given constraints (\"expire_on\" <= '2020-07-18') and ((\"expire_on\" at time zone 'EST')::date >= '2020-07-16'::date and (\"expire_on\" at time zone 'EST')::date <= '2020-07-18'::date) and ((\"claim_number\" = 'foo20') or (\"referred_to\" = '21')) and exception is: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement \"SELECT \"\"id\"\" FROM \"\"task\"\" WHERE \"\"id\"\" IN ('1') AND (\"\"expire_on\"\" <= '2020-07-18') AND ((\"\"expire_on\"\" AT[*] TIME ZONE 'EST')::DATE >= '2020-07-16'::DATE AND (\"\"expire_on\"\" AT TIME ZONE 'EST')::DATE <= '2020-07-18'::DATE) AND ((\"\"claim_number\"\" = 'foo20') OR (\"\"referred_to\"\" = '21')) \"; expected \"(,.,[,::,*,/,%,+,-,||,~,!~,NOT,LIKE,ILIKE,REGEXP,IS,IN,BETWEEN,AND,OR,)\"; SQL statement:\nSELECT \"id\" FROM \"task\" WHERE \"id\" IN ('1') and (\"expire_on\" <= '2020-07-18') and ((\"expire_on\" at time zone 'EST')::date >= '2020-07-16'::date and (\"expire_on\" at time zone 'EST')::date <= '2020-07-18'::date) and ((\"claim_number\" = 'foo20') or (\"referred_to\" = '21')) [42001-197]","TimeStamp":"2020-08-13 15:16:01.063","api_app_log":"io.inbox.stream.db.DBOperations"}

我什至在Postgres上运行,打印的查询工作正常。我找不到h2的问题。

解决方法

时区说明符… AT TIME ZONE …… AT LOCAL仅在H2 1.4.200起可用。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...