以下の Favorites テーブル オブジェクトを考えてみましょう。タイプ (以下で定義) で Favorites を見つけるためのクエリを書きたいと思います。また、FavoriteType をデータベースの String にマップする Typemapper も定義しました。
import scala.slick.driver.PostgresDriver.simple._
//Other imports have been omitted in this question
object Favorites extends Table[Favorite]("favorites") {
// Convert the favoriteTypes to strings for the database
implicit val favoriteMapping: TypeMapper[FavorietType] = MappedTypeMapper.base[FavorietType, String](
favType => FavorietType.values.find(_ == favType).get.mapping,
mapping => FavorietType.values.find(_.mapping == mapping).get
)
def favoriteType = column[FavoriteType]("type")
//other columns here
これは私が書きたいクエリです(ただし、コンパイルされません)
def queryByFavoriteType(ftype : FavoriteType)(implicit s: Session) = {
for(
f <- Favorieten if f.favoriteType === ftype
) yield f
}
}
ここで、さまざまな FavoriteType オブジェクトを定義しました (これは Favorieten オブジェクトの外側にあります)。
sealed case class FavorietType(mapping: String) {
override def toString = mapping.capitalize
}
object FavoriteType {
object Exam extends FavoriteType("examen")
object Topic extends FavoriteType("onderwerp")
object Paper extends FavoriteType("profielwerkstuk")
val values = Seq(Exam , Topic , Paper )
}
ここでの問題は、クエリがコンパイルされないことです。
value === is not a member of scala.slick.lifted.Column[models.gebruiker.FavorietType]
=== を使用してユーザー定義型を比較できないようですが、これは本当ですか? これを行う別の方法はありますか?
編集
関連する問題: 明示的な型のない TypeMapper を使用する前は、次のように定義されていましたimplicit val favoriteMapping = MappedTypeMapper.base[FavorietType, String]( ...
次のような FavoriteType.Exam (たとえば) を比較するクエリを作成する場合
def queryByFavoriteExam()(implicit s: Session) = {
for(f <- Favorieten if f.favorietType === FavorietType.Exam) yield f
}
これにより、エラーが発生しますcould not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[models.gebruiker.FavorietType.Exam.type]
。これに対する解決策は、以下に示すものと同じです。