【问题标题】:Slick 3.1.1 sql PSQLException: ERROR: syntax error at or near "" + ""Slick 3.1.1 sql PSQLException: ERROR: syntax error at or near "" + ""
【发布时间】:2016-12-25 22:45:52
【问题描述】:

我有以下导致上述错误的简单查询(并在 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)

【问题讨论】:

    标签: postgresql scala slick


    【解决方案1】:

    您的字符串是错误的 - 这是 Scala 中多行字符串的处理方式:https://www.safaribooksonline.com/library/view/scala-cookbook/9781449340292/ch01s03.html

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2020-05-25
      • 2014-05-02
      相关资源
      最近更新 更多