Scala “光滑的”===&引用;只为理解而编译
我遇到了一个奇怪的行为与滑头,我想得到一些帮助,找出它发生的原因。问题是,我有一个查询,内容如下:Scala “光滑的”===&引用;只为理解而编译,scala,slick,slick-2.0,Scala,Slick,Slick 2.0,我遇到了一个奇怪的行为与滑头,我想得到一些帮助,找出它发生的原因。问题是,我有一个查询,内容如下: db.withSession { implicit session => tables.users.where(_.id === UserId(1)).firstOption } inferred type arguments [Boolean] do not conform to method where's type parameter bounds [T <: scala.
db.withSession { implicit session =>
tables.users.where(_.id === UserId(1)).firstOption
}
inferred type arguments [Boolean] do not conform to method where's type parameter bounds [T <: scala.slick.lifted.Column[_]]
class Users(tag: Tag) extends Table[User](tag, "users") {
def id = column[UserId]("id", O.PrimaryKey, O.AutoInc, O.NotNull)
}
这不会编译生成如下错误:
db.withSession { implicit session =>
tables.users.where(_.id === UserId(1)).firstOption
}
inferred type arguments [Boolean] do not conform to method where's type parameter bounds [T <: scala.slick.lifted.Column[_]]
class Users(tag: Tag) extends Table[User](tag, "users") {
def id = column[UserId]("id", O.PrimaryKey, O.AutoInc, O.NotNull)
}
我有一个隐式转换来映射UserId类型:
implicit lazy val userIdColumnType = MappedColumnType.base[UserId, Int](_.value, UserId(_))
这看起来像是一个类型推断问题,但我真的不明白为什么会发生
有人知道为什么在我报告的两个场景中,这种行为会有所不同吗
编辑:经过一些调查,我发现当使用
where
时,userIdColumnType
的隐式转换必须在范围内,而对于理解来说则不需要。对此有很好的解释吗?您使用的是来自ScalaTest的===。它返回一个布尔值。Slick的===返回一个列[布尔]
。方法过滤器和其中
防止使用Boolean(至少在最新版本的Slick中是这样),以防止意外使用=
,或者在您的案例中也防止使用ScalaTest的==
,它对基础值进行本地比较,而不是数据库中的相等比较,这才是你真正想要的。对于理解,使用filter
将其分解为,有时会生成布尔值,因此不幸的是,对于理解,我们不能禁止布尔值
要解决这个问题,您需要确保在查询中选择了Slick的=
。也许您可以通过导入顺序或范围来影响这一点。或者,如果你运气不好,你不能,他们是不相容的
目前我不确定userIdColumnType在这里是如何交互的。我通过导入驱动程序api并确保它在作用域内解决了这个问题
i、 e.由于我使用的是postgresql驱动程序,
导入PostgresProfile.api.\u
您是否从其他库导入===呢?谢谢您的回答!是的,它是由ScalaTest添加的,因为该代码是我正在编写的一些测试的一部分;这就是问题所在吗?如果是,如果我将userIdColumnType
放在范围内,它的行为如何正确?