Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slick - Parameters are ignored in a Query Template

I'm trying to write a query template for like so:

val byIdentifier = for {
  (identifier, issuer) <- Parameters[(String, String)]
  o <- Objects if (o.identifier === identifier) && (o.identifierIssuer === issuer)
} yield o

And then use it like this:

def findByIdentifier(id: String, issuer: String): Option[Object] = 
  byIdentifier(id, issuer).firstOption

Objects is a Table with lifted embedding:

case class Object(val identifer: String, val identifierIssuer: String)

object Objects extends Table[Object]("objects") {
  def identifier = column[String]("identifier")
  def identifierIssuer = column[String]("identifier_issuer")
  ...
}

The problem I'm experiencing is that findByIdentifier("asdf", "weqr") produces a query with a where clause that is just false:

select ... from "objects" where false

What am I missing in my query template to have proper querying?

like image 779
ak. Avatar asked Nov 11 '22 18:11

ak.


1 Answers

Try the following syntax:

val byIdentifier = for {
  (identifier, issuer) <- Parameters[(String, String)]
  o <- Objects
  if o.identifier === identifier
  if o.identifierIssuer === issuer
} yield o

I often find that slick can be a bit wonky with its syntax, and the current documentation isn't particularly good.

You could also make your findByIdentifier do the work:

def findByIdentifier(id: String, issuer: String): Option[Object] = {
    Query(Object).where(_.identifier === id).where(_.identifierIssuer === issuer).run.headOption
}

I hope that helps...

like image 180
Mike Cialowicz Avatar answered Nov 14 '22 22:11

Mike Cialowicz