Brent Foust Brent Foust - 1 month ago 5x
Scala Question

Case-Insensitive String Comparison in Slick?

What is the proper way of finding the one User matching username?

With user-defined type User:

case class User (userId: String, username: String)

object User extends Table[User]("user") {
def userId = column[String]("userId", O.PrimaryKey)
def username = column[String]("username")
def * = userId ~ authId ~ username <>(User.apply _, User.unapply _)

Database.forDataSource(DB.getDataSource()) withSession {
implicit session: Session =>

val q = for { u <- User if u.username.equalsIgnoreCase(someUsername) }
yield u

user.username is of type Column[String] which has no conversion to String.

What is desired is to have the Database do the string-insensitive comparison as part of the query.


I had a similar situation and solved it by using toLowerCase extension method:

p <- if p.loginName.toLowerCase === partyName.toLowerCase

You can find here more extension methods, especially String ones.