0
votes

I have recently started learning Scala and the Play Framework, and as I was going through the Anorm documention for the Play Framework, I came to the following code snippet:

import anorm.SqlParser.str

val id: List[String] = 
  SQL("insert into City(name, country) values ({name}, {country})")
 .on('name -> "Cambridge", 'country -> "New Zealand")
 .executeInsert(str.+) // insertion returns a list of at least one string keys

and got stuck with the compilation error:

ambiguous reference to overloaded definition, both method str in object SqlParser of type (columnPosition: Int)(implicit c: anorm.Column[String])anorm.RowParser[String] and method str in object SqlParser of type (columnName: String)(implicit c: anorm.Column[String])anorm.RowParser[String] match expected type ?

I am using a regular Scala Play seed with a PostgreSQL database and modified the HomeController index action:

package controllers
import play.api.Play.current
import javax.inject._
import play.api._
import play.api.mvc._
import play.api.db.{ Database, NamedDatabase, DB }
import anorm._
import anorm.SqlParser.str

@Singleton
class HomeController @Inject()() extends Controller { 

  def index = Action {
   DB.withConnection { implicit c =>
     val id: List[String] = 
       SQL("insert into City(name, country) values ({name}, {country})")
       .on('name -> "Cambridge", 'country -> "New Zealand")
       .executeInsert(str.+) // insertion returns a list of at least one string keys  
     Ok("Result is: " + id)
   }       
  }

}

Here are the build dependencies:

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
  "com.typesafe.play" %% "anorm" % "2.5.0",
  "org.postgresql"     %  "postgresql" % "9.3-1102-jdbc41"
)

What is the problem?

1

1 Answers

0
votes

You are missusing .str, which must be called either with a column position or a column name, in order to parse the so referenced column as a string.