"ambiguous reference to overloaded definition" 对于 Anorm 的含义
Meaning of "ambiguous reference to overloaded definition" for Anorm
我最近开始学习 Scala 和 Play Framework,在学习 Anorm documention for the Play Framework 时,我得到了以下代码片段:
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
并遇到编译错误:
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 ?
我正在使用带有 PostgreSQL 数据库的常规 Scala Play 种子并修改了 HomeController 索引操作:
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)
}
}
}
以下是构建依赖项:
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"
)
有什么问题?
您误用了 .str
,必须使用列位置或列名称调用它,以便将如此引用的列解析为字符串。
我最近开始学习 Scala 和 Play Framework,在学习 Anorm documention for the Play Framework 时,我得到了以下代码片段:
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
并遇到编译错误:
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 ?
我正在使用带有 PostgreSQL 数据库的常规 Scala Play 种子并修改了 HomeController 索引操作:
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)
}
}
}
以下是构建依赖项:
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"
)
有什么问题?
您误用了 .str
,必须使用列位置或列名称调用它,以便将如此引用的列解析为字符串。