光滑3.1.1 sql PSQLException:错误:语法错误在“”+“”

时间:2016-12-25 14:42:47

标签: postgresql scala slick

我有以下简单(并在Postgres中测试)查询导致上述错误,我找不到它是什么。

def findActiveByProviderKeyAndEmail(providerKey: String, email: String): Future[Option[UserRow]] = {
  val action = sql"""SELECT t1.* FROM user t1 " +
                     "WHERE t1.active=true AND " +
                     "      t1.email=$email AND " +
                     "      EXISTS (SELECT * FROM linked_account t2 " +
                     "              WHERE t2.user_id=t1.id AND " +
                     "                    t2.provider_key=$providerKey)""".as[UserRow].headOption
  db.run(action)
}

请注意,在我使用${User.baseTableRow.tableName}代替user${LinkedAccount.baseTableRow.tableName}代替linked_account之前,请将其删除以排除错误的可能性。

完整的运行时错误如下:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PSQLException: ERROR: syntax error at or near "" +
                       ""
  Position: 26]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "" +
                       ""
  Position: 26
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
        at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
        at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168)
        at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:157)
        at com.zaxxer.hikari.proxy.PreparedStatementProxy.execute(PreparedStatementProxy.java:44)
        at com.zaxxer.hikari.proxy.PreparedStatementJavassistProxy.execute(PreparedStatementJavassistProxy.java)
        at slick.jdbc.StatementInvoker.results(StatementInvoker.scala:39)

1 个答案:

答案 0 :(得分:1)

您的字符串错误 - 以下是Scala中多行字符串的完成方式:{​​{3}}

换句话说,它应该是这样的:

def findActiveByProviderKeyAndEmail(providerKey: String, email: String): Future[Option[UserRow]] = {
  val action = sql"""SELECT t1.* FROM user t1
                     WHERE t1.active=true AND
                           t1.email=$email AND
                           EXISTS (SELECT * FROM linked_account t2
                                   WHERE t2.user_id=t1.id AND
                t2.provider_key=$providerKey)""".as[UserRow].headOption
db.run(action)
}