1

I'm using Slick 3.1.1 and a case class to represent the mapping with an existing table and I'm trying to update a row. One way I found doing this is by specifying which fields to update like so:

val query = table.filter(_.id == id)
                 .map(r => (r.field1, r.field2))
                 .update(("value1", "value2"))
db.run(query)

The table has a few more fields. Now the problem is that the whole row comes into this method and some fields remain unchanged. I saw on a related question that in Slick 2.X the following was an option:

table.filter(_.id === id).update(row)

Trying the above however naturally produced a Cannot update identity column 'id' error.

Is there a way I can do an update without having to specify specific fields whilst at the same time leaving out the id?

Thanks!

Community
  • 1
  • 1
Maikon
  • 1,382
  • 16
  • 16
  • What RDBMS are you using? – Paweł Jurczenko Aug 03 '16 at 15:16
  • Hey Pawel. It's MS SQL Server. – Maikon Aug 03 '16 at 15:17
  • 2
    Slick itself allows presence of IDs during updates (after all it just converts our code to SQL queries) and your code would work fine if executed against e.g. PostgreSQL. I don't know much about MS SQL Server, but its `IDENTITY` property is the problem here. Would it be possible for you to define your primary keys without that property? You might check this post: http://stackoverflow.com/a/17249583/4838717 for the details. – Paweł Jurczenko Aug 03 '16 at 15:27
  • Yeah I was thinking that might be the problem..I'll have a look at the post you suggested. Thanks Pawel! – Maikon Aug 03 '16 at 15:48
  • Did you ever find a good solution for this issue? – preston.m.price Mar 01 '17 at 19:39
  • @preston.m.price Unfortunately not. I ended up specifying the fields that were being updated. – Maikon Mar 02 '17 at 13:13

0 Answers0