简体   繁体   English

Slick 3.1.1模糊隐含值涉及过滤器和隐式CanBeQueryCondition

[英]Slick 3.1.1 ambiguous implicit values involving filter and implicit CanBeQueryCondition

I have been trying to create a Generic Dao over Slick 3.1.1 and it includes a generic filter that competes with JPA's findByExample , see the following files: 我一直在尝试通过Slick 3.1.1创建Generic Dao,它包含一个与JPA的findByExample竞争的通用过滤器,请参阅以下文件:

In this last file I try to use the generic filter function to find a user by its registered email, like this: 在最后一个文件中,我尝试使用通用过滤器功能通过其注册的电子邮件查找用户,如下所示:

// 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
}

but this produces the compiler error: 但这会产生编译错误:

[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]                   ^

Can anyone advice on how the implicit declaration of the filter function below can be improved to solve this compiler error? 任何人都可以建议如何改进下面的filter函数的隐式声明来解决这个编译错误?

The implementation of the filter function (found in GenericDaoImpl.scala ) is: 过滤函数的实现(在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)

As far as I can see you are simply lacking you profile API import in UserService . 据我所知,你只是缺少UserService配置文件API导入。

Just add there this import: import profile.api._ and it should work. 只需添加此导入: import profile.api._ ,它应该工作。

EDIT: BTW I see many people building their own version of base CRUDs for Slick. 编辑:BTW我看到很多人为Slick构建自己版本的基础CRUD。 Did you try some existing thin libraries doing just that eg here: https://github.com/VirtusLab/unicorn ? 您是否尝试过一些现有的瘦库,例如: https//github.com/VirtusLab/unicorn It's not really related to this question but it may be worth to take a look. 这与这个问题没有关系,但值得一看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM