Slick MappedTo [String] column cannot be used as

I want to use MappedTo [String] as one type of column. There is one query, for example, to select some entries.

Code example:

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
}

Compiler Error:

[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

How to fix it? Thank.

+4
source share
2 answers

The only solution I found now is to access 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
}

There is also a startsWith method as a side element , which can be used when filtering strings with a raised attachment, and this would be more suitable for your business; that is, if the filter worked properly.

0
source

. .

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

Source: https://habr.com/ru/post/1544542/


All Articles