postgresqlscalaanorm

Executing update using Anorm returns PSQLException: The column index is out of range: 2, number of columns: 1


I have been trying to execute an update query for my PostgreSQL database using Anorm with Play Framework 2.6 in Scala. The query works fine in pgAdmin so I am not sure what is going wrong here. I only want to update a specific column of an entry. The wordlistcollection table holds 3 columns: id, title and createddate.

I have tried using both execute() and executeUpdate() as well as adding all required columns but without any success.

override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
    SQL"""
      UPDATE wordlistcollection
      SET title = '${wordListCollection.title}'
      WHERE id = ${wordListCollection.id};
    """.executeUpdate()
  }

EDIT: I've also tried this approach, same result

override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
    SQL"""
      UPDATE wordlistcollection
      SET title = {title}
      WHERE id = {id}
    """
    .on(
        "id" -> wordListCollection.id,
        "title" -> wordListCollection.title)
    .executeUpdate()
  }

According to the executeUpdate() function it should return the number of rows affected as Integer, but it returns the following:

! @7c21ckgga - Internal server error, for (PUT) [/api/lists] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PSQLException: The column index is out of range: 3, number of columns: 2.]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:382)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:380)
    at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:412)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
    at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:65)
    at org.postgresql.core.v3.SimpleParameterList.setBinaryParameter(SimpleParameterList.java:132)
    at org.postgresql.jdbc.PgPreparedStatement.bindBytes(PgPreparedStatement.java:983)
    at org.postgresql.jdbc.PgPreparedStatement.setLong(PgPreparedStatement.java:279)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setLong(HikariProxyPreparedStatement.java)
    at anorm.ToStatementPriority0$longToStatement$.set(ToStatementMisc.scala:197)
    at anorm.ToStatementPriority0$longToStatement$.set(ToStatementMisc.scala:196)
    at anorm.DefaultParameterValue.set(ParameterValue.scala:40)
    at anorm.SimpleSql.$anonfun$unsafeStatement$3(SimpleSql.scala:84)
    at anorm.SimpleSql.$anonfun$unsafeStatement$3$adapted(SimpleSql.scala:84)

I think it has something to do with the returned ResultSet, but I am quite a beginner so no idea how to debug this.

Thanks for your help!


Solution

  • So, turns out I did two things wrong. As @cchantep pointed out, there should not be single quotes surrounding a string when using string interpolation for building up your SQL queries. Removing these quotes fixed the problem.

     override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
        SQL"""
          UPDATE wordlistcollection
          SET title = ${wordListCollection.title}
          WHERE id = ${wordListCollection.id}
        """
        .executeUpdate()
      }
    

    Weirdly the approach using

    override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
        SQL"""
          UPDATE wordlistcollection
          SET title = {title}
          WHERE id = {id}
        """
        .on(
            "id" -> wordListCollection.id,
            "title" -> wordListCollection.title)
        .executeUpdate()
      }
    

    Did not work, it gave me another exception.