Play Slick: Like with an Option[String] column

43 views
Skip to first unread message

Antonio reKiem

unread,
Jul 31, 2017, 1:08:51 AM7/31/17
to Slick / ScalaQuery

How can I make a query with a like when the object is a column[Option[String]]?

This is my model:

val id: Rep[Int] = column[Int]("id", O.AutoInc, O.PrimaryKey)
val ownerName: Rep[String] = column[String]("owner_name")
val membershipCode: Rep[Option[String]] = column[Option[String]]("membership_code")

And this is my query:

val query = orderTableQuery.filter { order =>
  Seq(
    search.filter(!_.isEmpty).map(filterString => {
      val searchTerm = s"%$filterString%".toLowerCase
      Seq(
//            Some(order.membershipCode.toLowerCase like searchTerm), // This is the one causing problems
        Some(order.ownerName.toLowerCase like searchTerm) // This works fine
      ).flatten.reduceLeftOption(_ || _).getOrElse(true: Rep[Boolean])
    }),
    // ...other optional filters
  ).flatten.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
}

However, when I try to remove the comment tags from that line, the type of the sequence changes, so i can not make the reduceLeftOption(_ || _) part, if I add a get inside the option column then it compile fine, but when i try the query slick throws an error because you can not use a get in the database (which for me makes sense), but then, how can I handle this Option[String] column?


I have already post this question in SO with no luck, any guide will be appreciated.


I have tried some things, like:

  1. Compile fine, but in execution throws exception in the get

    Some(order.membershipCode.get.toLowerCase.like(searchTerm))

  2. The reduceLeftOption(_ || _) does not work anymore

    Some(order.membershipCode.toLowerCase.like(searchTerm))

  3. Type mismatch

    Some(order.membershipCode.toLowerCase.like(Some(searchTerm)))

Richard Dallaway

unread,
Jul 31, 2017, 3:33:51 AM7/31/17
to Slick / ScalaQuery
The workaround I've seen for this is to use asColumnOf. E.g.,

membershipCode.asColumnOf[String].toLowerCase like searchTerm

Anyone have a better way?

Ticket https://github.com/slick/slick/issues/1664 is looking for someone to work on a more general solution.

Richard

--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/ed9a1f68-fb64-4508-bbc9-b1c5ca0bf86e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonio reKiem

unread,
Jul 31, 2017, 5:36:43 PM7/31/17
to Slick / ScalaQuery
Thanks a lot for the info, i have been strugling with this for some days, would you like to answer my question in SO? or if there is no problem for you I can answer for myself and link here.
In the meantime i guess ill be checking the issue you mention.
Reply all
Reply to author
Forward
0 new messages