@since
might work if anybody has experience implementing that with Slick
I have a use case where I have an optional column, and I want to write a filter that checks for a value when it's present or check for null it it's null.
I initially wrote:
table.filter(_.name === name)
In this case, when None
, the query generates name = null
, which is not correct, I was expecting name is null
, so I had to change my filter to:
table
.filterOpt(name) { case (t, n) => t.name === n }
.filterIf(name.isEmpty)(_.name.isEmpty)
That produces the right query, but it seems like I should get that behaviour for free when using an optional column. Those two filter are mutually exclusive.
Is there a primitive I should be using instead?
Ah sorry I did not understand properly. Would it be
table.filter(name match {
case None => _.name.isEmpty
case Some(actualName) => _.name === actualName
})
? I'm unsure whether Scala will correctly infer the type of that filtering function, so you might have to help it a bit.
Model class & Table class are respectively. I am not able to understand compilation failure with insert Query. Can someone help ?
case class Task(id: Int = -1, text: String, project: Option[String], context: Option[String], completed: Boolean)
class Tasks(tag: Tag) extends Table[Task](tag, "tasks"){
def id: Rep[Int] = column[Int]("id")
def text: Rep[String] = column[String]("text")
def project: Rep[Option[String]]= column[Option[String]]("project")
def context: Rep[Option[String]] = column[Option[String]]("context")
def isCompleted: Rep[Boolean] = column[Boolean]("isCompleted")
override def * = (id, text, project, context, isCompleted) <> ( Task.tupled, Task.unapply)
}
InsertQuery looks like this now.
val insertQuery= tasks += newTask
A lot of examples are in similar way but I could not figure out the difference here for a couple of hours now.
export
might be able to solve that, not sure
Task
(as quoted in the snippet above) along with DAO/Repository. Now that we spoke of H2 (only for testing), question I am getting is - is it a good idea to test with H2 when the production is going to be something else. Kind of beginner with slick - not sure if this advisable. If so - any directions ? Thanks