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
SchemaGenerator
is needed. SchemeGenerator
requires Database
and collection Table[Model]
. There is also this sticky import statement import slick.jdbc.PostgresProfile.api._
which I want to keep it min files as possible. Given these I am looking for design these classes.
The question is why you want to keep that import statement in as min files as possible? If it's because your project is the barebones for building other projects, then you can always store somewhere in a global object the profile you want to use. Something like
object DatabaseGlobals {
def profile = slick.jdbc.PostgresProfile
}
and then you can import that instead, via my.package.DatabaseGlobals.profile.api._
.
If the reason is for mocks in tests, then I think you can put your table definitions inside a trait which requires an abstract jdbc profile, and feed that profile by extending the trait (I think that works)