gpt4 book ai didi

scala - Slick 3.0 DAO 编译失败

转载 作者:行者123 更新时间:2023-12-01 11:32:33 28 4
gpt4 key购买 nike

我今天开始学习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

我的代码看起来是正确的。所以我不知道为什么它不能编译。我发现像“模棱两可的隐式值”和“无法执行选项映射操作”这样的错误在这个时候有点困难。

最佳答案

如果您仔细观察,答案就在错误中。您正在将 Int 列与 Long 值进行比较。您可以显式转换列(反之亦然):

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

关于scala - Slick 3.0 DAO 编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31057153/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com