Slick 3.0 DAO无法编译

时间:2015-06-25 17:41:37

标签: scala playframework slick

我今天开始学习光滑的3.0。我正在尝试编写一个DAO,这是我编写的代码

package tables

import slick.driver.H2Driver.api._
import play.api.Play.current
import scala.concurrent._
import ExecutionContext.Implicits.global._
import play.api.db.DB

case class Supplier(id: Int, name: String, street : String, city : String, state : String, zip : String)

class Suppliers(tag: Tag) extends Table[Supplier](tag, "SUPPLIERS") {
  def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
  def name = column[String]("SUP_NAME")
  def street = column[String]("STREET")
  def city = column[String]("CITY")
  def state = column[String]("STATE")
  def zip = column[String]("ZIP")
  // Every table needs a * projection with the same type as the table's type parameter
  def * = (id, name, street, city, state, zip) <> (Supplier.tupled, Supplier.unapply _)
}

object SupplierDAO {

  val suppliers = TableQuery[Suppliers]

  def db: Database = Database.forDataSource(DB.getDataSource())

  def filterQuery(id: Long): Query[Suppliers, Supplier, Seq] =
    suppliers.filter(x => x.id === id)

  def findById(id: Long): Future[Supplier] =
    try db.run(filterQuery(id).result.head)
    finally db.close

  def insert(supplier: Supplier): Future[Int] =
    try db.run(suppliers += supplier)
    finally db.close

  def update(id: Long, supplier: Supplier): Future[Int] =
    try db.run(filterQuery(id).update(supplier))
    finally db.close

  def delete(id: Long): Future[Int] =
    try db.run(filterQuery(id).delete)
    finally db.close
}

但是我遇到了两个编译器错误消息,这些消息对我来说非常深奥。

[error] /Users/abhi/ScalaProjects/play-reactive-slick/app/tables/Suppliers.scala:29: Cannot perform option-mapped operation
[error]       with type: (Int, Long) => R
[error]   for base type: (Int, Int) => Boolean
[error]     suppliers.filter(x => x.id === id)
[error]                                ^
[error] /Users/abhi/ScalaProjects/play-reactive-slick/app/tables/Suppliers.scala:29: ambiguous implicit values:
[error]  both value BooleanColumnCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[slick.lifted.Rep[Boolean]]
[error]  and value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[slick.lifted.Rep[Option[Boolean]]]
[error]  match expected type slick.lifted.CanBeQueryCondition[Nothing]
[error]     suppliers.filter(x => x.id === id)
[error]                     ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed Jun 25, 2015 12:54:20 PM
Mohitas-MBP:play-reactive-slick abhi$ 

我已经在网上看了很多这样的教程

http://blog.knoldus.com/2015/03/03/play-with-reactive-slick-a-simple-crud-application-in-play-framework-using-slick-3-0/

http://slick.typesafe.com/doc/2.1.0/gettingstarted.html

我的代码对我来说是正确的。所以我不知道它为什么不编译。在这个时间点,我发现像“模糊隐含值”和“无法执行选项映射操作”这样的错误。

1 个答案:

答案 0 :(得分:5)

如果你看起来足够努力,答案就是错误。您将Int列与Long值进行比较。您可以显式转换列(反之亦然):

def filterQuery(id: Long): Query[Suppliers, Supplier, Seq] =
  suppliers.filter(x => x.id.asColumnOf[Long] === id)