Slick Map To [String]列可以使用like

时间:2014-06-14 00:09:27

标签: slick

我想将MappedTo [String]用作一个列类型。有一个查询使用like来选择一些记录。

示例代码:

case class ID(value: String) extends scala.slick.lifted.MappedTo[String]

class Order(tag: Tag) extends Table[(ID, String)](tag, "order") {
  def id = column[ID]("id", O.PrimaryKey)
  def name = column[String]("name", O.NotNull)
  def * = (id, name)
}

val ordres = TableQuery[Order]

def all(implicit session: Session) = {
  ordres.filter(_.id like "2014.%").list
}

编译错误:

[error]  value like is not a member of scala.slick.lifted.Column[models.ID]
[error]     ordres.filter(_.id like "2014.%").list
[error]                        ^
[error]  ambiguous implicit values:
[error]  both value BooleanColumnCanBeQueryCondition in object CanBeQueryCondition of type => scala.slick.lifted.CanBeQueryCondition[scala.slick.lifted.Column[Boolean]]
[error]  and value BooleanOptionColumnCanBeQueryCondition in object CanBeQueryCondition of type => scala.slick.lifted.CanBeQueryCondition[scala.slick.lifted.Column[Option[Boolean]]]
[error]  match expected type scala.slick.lifted.CanBeQueryCondition[Nothing]
[error]     ordres.filter(_.id like "2014.%").list

如何解决?感谢。

2 个答案:

答案 0 :(得分:0)

我目前找到的唯一解决方案是诉诸sql interpolated strings

//slick needs this to be able to convert to your custom wrapper type
implicit val GetID = GetResult(r => ID(r.nextString))

def all(implicit session: Session) = {
  sql"""select id, name from order where id like '2014.%'""").list
}

作为旁注,还有一个startsWith方法可以用于过滤嵌入字符串,并且它更适合你的情况;也就是说,如果过滤器按预期工作。

答案 1 :(得分:-2)

您应该覆盖值定义。见this answer

case class ID(id: String) extends scala.slick.lifted.MappedTo[String] {
   override def value: String = id
}