【问题标题】:Meaning of "ambiguous reference to overloaded definition" for AnormAnorm 的“对重载定义的模糊引用”的含义
【发布时间】:2016-11-23 12:06:19
【问题描述】:

我最近开始学习Scala和Play Framework,在翻Anorm documention for the Play Framework的过程中,得到如下代码sn-p:

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

遇到编译错误:

对重载定义的模糊引用, 类型为 (columnPosition: Int)(隐式 c: anorm.Column[String])anorm.RowParser[String] 的对象 SqlParser 中的两个方法 str 和类型为 (columnName: String)(implicit c: anorm.Column[String])anorm.RowParser[String] 的对象 SqlParser 中的方法 str 匹配预期类型?

我正在使用带有 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"
)

有什么问题?

【问题讨论】:

    标签: scala anorm


    【解决方案1】:

    您误用了.str,必须使用列位置或列名调用它,才能将如此引用的列解析为字符串。

    【讨论】:

    • 但是示例来自文档页面?我看到重载是在 2014 年添加的。也许文档很旧?
    猜你喜欢
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多