For example I have a Table and associated mapped class
case class MyModel(
myPk: Int,
someText: String
)
class MyTable(tag: Tag) extends Table[MyModel](tag, "my_table") {
def myPk: Rep[Int] = column("my_pk")
def someText: Rep[String] = column("some_text")
def * = (myPk, someText) <> ((MyModel.apply _).tupled, MyModel.unapply)
def pk = primaryKey("my_pk_pk", myPk)
}
I have a migration that changes the myPk
column from an integer to a varchar, so I now need to somehow represent that change within my table and case class with the option to roll back. The actual DB rollback is handled (rolled my own migration framework based off of Forklift) but it's handling the code rollback that is alluding me.
I could duplicate MyTable
with myPk
as an integer and then also duplicate MyModel
using myPk
as an int still but then I'm having to potentially weave these two competing tables and models through the code depending on whether we've migrated or rolled back...
MyModel
with myPk
as a String
as well as MyTable
with myPk
as a String
/varchar
@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