2

I have a set of simple case classes, each of which have a number of properties that are Optional:

case class Person (name: Option[String], age: Option[Int], etc)

When all case class properties are provided (not-None) the slick update code works fine - I just use the case class instance in the update query.

The question is, there are many cases which any combination of properties might be None. I don't want to write a specific update query method for each combination.

How do I use a case class in an slick update query so slick only updates the non-None property values in the table and leaves the others intact (not trying to set them to null)?

1
  • 1
    If you want to update something partially you just use "copy" method for any case class and override what you need. Not sure if it's a solution to your problem, specific example is needed :)
    – sap1ens
    Commented Jun 30, 2015 at 23:11

1 Answer 1

2

You can update specify columns using Slick:

http://slick.typesafe.com/doc/3.0.0/queries.html#updating

but to achieve what you want there I'd do it with two very simple DB calls:

val row = db.run(q.filter(_.id === id).result.head)
row.copy(name = row.name.map(newName), age = row.age.map(newAge), ...)
db.run(q.update(row))

note the use of .map to set it only if it was previously defined.

Not the answer you're looking for? Browse other questions tagged or ask your own question.