Correct use of the stain filter
I am using slick to access the 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 it might work. However, the compiler tells me that the error is: (126, -1) Play 2 Compiler: type mismatch; Found: Required: slick.lifted.Rep [?]
The problem is this: shopId.map (s => c.shopId === s) .getOrElse (true)
I am wondering how I can make this work.
I am using slick 3.0.0-RC
+4
source to share
2 answers
As mentioned in this answer, you can use the following API starting with Slick version 3.3.0 :
def findCouponBy(couponId: Long, shopId: Option[String]) = {
val query =
this
.filter(_.couponId === couponId)
.filterOpt(shopId){ case (table, sid) =>
table.filter(_.shopId === sid)
}
db run query.result
}
0
source to share