我有一个案例类,映射和像这样定义的对象
import slick.driver.PostgresDriver.api._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
case class Coffee(name : String, supID: Int, price: Double, sales: Int, total: Int) {
def save(): Coffee = { Coffees.save(this) }
def delete() { Coffees.delete(this) }
}
class Coffees(tag: Tag) extends Table[Coffee](tag, "coffee") {
def name = column[String]("cof_name", O.PrimaryKey)
def supID = column[Int]("sup_id")
def price = column[Double]("price")
def sales = column[Int]("sales", O.Default(0))
def total = column[Int]("total", O.Default(0))
def * = (name, supID, price, sales, total) <> (Coffee.tupled, Coffee.unapply)
}
object Coffees extends TableQuery(new Coffees(_)) {
lazy val db = DatabaseAccess.databases("db.test")
def save(coffee: Coffee): Coffee = {
val saveQuery = (this returning this).insertOrUpdate(coffee)
Await.result(db.run(saveQuery), Duration.Inf) match {
case Some(x) => x
case None => coffee
}
}
def delete(coffee: Coffee) = {
Await.result(db.run(this.filter(_.name === coffee.name).delete), Duration.Inf)
}
}
现在我想编写其他类的查询:
import com.typesafe.config.ConfigFactory
import org.scalatest.{FeatureSpec, GivenWhenThen, Matchers}
class CoffeeSpec extends FeatureSpec with GivenWhenThen with Matchers {
feature("Accessing coffees in the database") {
scenario("Adding a new coffee") {
val config = ConfigFactory.load()
DatabaseAccess.loadConfiguration(config)
lazy val db = DatabaseAccess.databases("db.test")
val coffee = Coffee("nescafe", 1, 20, 20, 40).save()
val temp = Coffees.filter(_.name === "nescafe")
temp.length should be (1)
coffee.delete()
}
}
}
这一行
val temp = Coffees.filter(_.name === "nescafe")
引发如下错误:
在Coffees对象上编写过滤器查询的最佳方法是什么? 使用slick 2.0我有类似的查询:
Query(Coffees).filter(_.name is "coffee").firstOption.getOrElse(None)
我希望能够使用新设置对所有数据映射执行类似的查询。
为什么我会收到这些错误?我应该如何在光滑的3中进行类似的查询?
答案 0 :(得分:2)
您的查询没问题,但您的CoffeeSpec
文件缺少导入:
import slick.driver.PostgresDriver.api._
编译应该通过。