Filtering when using custom column type in Slick

六月ゝ 毕业季﹏ 提交于 2019-12-04 03:11:26

The thing is that the Scala compiler will look for an implicit convertion for Deleted.type instead of Status.

As Deleted is declared as an object it is not a class, its actual class is Deleted.type, so you just have to help the compiler to understand that is actually a Status. How? You can try

class MyTableDao {
val Items = TableQuery[MyTableDefinition]

def byId(id: Long)(implicit session: Session): Option[TableMapping] =
    Items.filter(_.status =!= Deleted.asInstanceOf[Status]).firstOption
}

That'll do it.

Let me know if it did work, I'm facing a similar problem and I was able to get rid of it doing that.

Your custom type mapper needs to be in scope for the DAO; I'd do something like:

trait MyTypeMapper {
  protected implicit val statusColumnType = 
    MappedColumnType.base[Status, Int](_.intValue, intToStatus)

  private def intToStatus(i: Int): Status = i match {
    case 1 => Active
    case 2 => Disabled
    case _ => Deleted
  }
}

and then mix the trait into your table mapper and dao:

class MyTableDefinition(tag: Tag) 
  extends Table[TableMapping](tag, "sometable")
  with MyTypeMapper {...}

class MyTableDao extends MyTypeMapper{...}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!