class Foos(tag: Tag) extends Table[Foo](tag, "foos") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def a = column[Option[String]]("a")
def b = column[String]("b")
// and some more fields, both optional and not
}
def findFoos(a: Option[String], b: Option[String]) = { // and some more parameters here
val query = (f: Foos) => List(
a.map(f.a === _),
b.map(f.b === _)
).flatten.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
Hi all,
I'm trying to use Slick 3.1.1 in my current project and I've faced with following problem. I have schema with optional fields:
class Foos(tag: Tag) extends Table[Foo](tag, "foos") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def a = column[Option[String]]("a")
def b = column[String]("b")
// and some more fields, both optional and not
}
In a service class I have optional parameters, since some of them can be not defined. I'm trying to reduce conditions to one query, like it shown in the Slick documentation, but it doesn't compile:
def findFoos(a: Option[String], b: Option[String]) = { // and some more parameters here
val query = (f: Foos) => List(
a.map(f.a === _),
b.map(f.b === _)
).flatten.reduceLeftOption(_ && _).getOrElse(true: Rep[Boolean])
The problem, this cannot be folded. As I understand the reason is that items have different types: Rep[Boolean] for not nullable fields and Rep[Option[Boolean]] for optionals. How should I use it properly?
--
Regards,
Mike.
--
---
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/d11d6c9d-e8a7-48ff-9b67-47d73c163a20%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
First write it out concrete, then see how you can pull out and abstract over the repetitiveness. In this case I believe that might involve a polymorphic (having a type parameter) function. But in order to identify what your can do, let's see how it looks written out concrete and repetitive.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/f0808b8d-2af8-47ec-b91a-21e0cd031b0c%40googlegroups.com.