Slick 3.1.1涉及过滤器和隐式CanBeQueryCondition

时间:2016-12-13 21:52:19

标签: scala slick implicit slick-3.0

我一直在尝试通过Slick 3.1.1创建Generic Dao,它包含一个与JPA findByExample竞争的通用过滤器,请参阅以下文件:

在最后一个文件中,我尝试使用通用过滤器功能通过其注册的电子邮件查找用户,如下所示:

// this will implicitly exec and wait indefinitely for the 
// db.run Future to complete
import dao.ExecHelper._ 

def findByEmail(email: String): Option[UserRow] = {
  userDao.filter(_.email === email).headOption
}

但这会产生编译错误:

[error] /home/bravegag/code/play-authenticate-usage-scala/app/services/UserService.scala:35: value === is not a member of String
[error]     userDao.filter(email === _.email).headOption
[error]                          ^
[error] /home/bravegag/code/play-authenticate-usage-scala/app/services/UserService.scala:35: ambiguous implicit values:
[error]  both value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[slick.lifted.Rep[Option[Boolean]]]
[error]  and value BooleanCanBeQueryCondition in object CanBeQueryCondition of type => slick.lifted.CanBeQueryCondition[Boolean]
[error]  match expected type slick.lifted.CanBeQueryCondition[Nothing]
[error]     userDao.filter(email === _.email).headOption
[error]                   ^

有人可以建议如何改进下面filter函数的隐式声明来解决这个编译错误吗?

过滤函数的实现(在GenericDaoImpl.scala中找到)是:

// T is defined above as T <: Table[E] with IdentifyableTable[PK]

override def filter[C <: Rep[_]](expr: T => C)
  (implicit wt: CanBeQueryCondition[C]) : Future[Seq[E]] = 
    db.run(tableQuery.filter(expr).result)

1 个答案:

答案 0 :(得分:3)

据我所知,您只是缺少UserService中的个人资料API导入。

只需在此处添加导入:import profile.api._即可。

编辑:BTW我看到很多人为Slick构建自己版本的基础CRUD。您是否尝试过一些现有的瘦库,例如在这:https://github.com/VirtusLab/unicorn?它与这个问题没有关系,但值得一看。

相关问题