I will not post the exact error message, because it is unfortunately in Polish (my postgresql has Polish locale). Anyway, as close as I can get in English is "column
"status" is of type enum_status but expression is of type character
varying". I believe that what postgresql (or jdbc driver) wants is cast from character varying to db enum type, like "CAST(xxx AS some_enum_type)" but I just can find no way to force Slick to add it to generated insert/update statements. I do not want to use plain sql queries nor implicit postgresql casts.
The problem with code below is it converts scala enum object to string, which is mapped into character varying instead of enum which is not what postgresql expects. Any help appreciated.
//Enum base
abstract class DBEnumString extends Enumeration {
implicit val enumMapper = MappedJdbcType.base[Value, String](_.toString(), s => this.withName(s))
}
//Actual enum object
object EntryStatus extends DBEnumString {
type T = Value
val A1 = Value("A1")
val A2 = Value("A2")
}
//Slick table definition
class UserEntries(tag: Tag) extends Table[(Int, Int, EntryStatus.T)](tag, "user_entries") {
def userId = column[Int]("user_id")
def entryId = column[Int]("entry_id")
def status = column[EntryStatus.T]("status")
def * = (userId, entryId, status)
}
val userEntries = TableQuery[UserEntries]
//Dao method - this is where exception is thrown
def addOrUpdateUserEntry(userId: Int, entryId: Int, entryStatus: EntryStatus.T)(implicit s: Session) = {
if (!userEntries.where(ue => { ue.userId === userId && ue.entryId === entryId }).exists.run) {
userEntries += (userId, entryId, entryStatus)
} else {
userEntries.where(ue => { ue.userId === userId && ue.entryId === entryId }).map(_.status).update(entryStatus)
}
}