scalaslickplayframework-2.3

right usage of slick filter


I'm using slick to access database. I want to query like this:

case class Coupon(couponId: Long, shopId: String)

class Coupons(tag: Tag) extends Table[Coupon](tag, "coupons"){

  def couponId = column[Long]("coupon_id")

  def shopId = column[String]("shop_id")

  override def * = (couponId, shopId) <> (Coupon.tupled, Coupon.unapply)
}

object Coupons extends TableQuery(new Coupons(_)){

  def findCouponBy(couponId: Long, shopId: Option[String]) = {

    val s = DB.createSession()
    try {
       val q = for {
           coupon <- this.filter(c => c.couponId === couponId && 
                shopId.map(s => c.shopId === s).getOrElse(true)
        } yield coupon
      s.database.run(q.result)
    } finally s.close
  }
}

I thought this might work. However, the compiler tells me that Error:(126, -1) Play 2 Compiler: type mismatch; found : Any required: slick.lifted.Rep[?]

Problem lies on here: shopId.map(s => c.shopId === s).getOrElse(true)

I'm wondering how I can make this work.

I'm using slick 3.0.0-RC


Solution

  • Use slick.lifted.LiteralColumn(true)

    Scala's type infer limitation