1

I have this code using slickdb:

val action: DBIOAction[Int, NoStream, Effect] =
  for {
    id <- sql"select id from product where name = $name".as[Int].head
    _  <- sql"update product set description = ${description(id, name)}".asUpdate if id != 5
  } yield id

db.run(action)

With this code, the action will not return the id if id != 5. This is not what I want. I want that the set description update is only executed if id != 5, and at the same time dbaction must return the id independenting or whether id != 5 or not. How can I achieve this?

David Portabella
  • 12,390
  • 27
  • 101
  • 182

1 Answers1

0

You might need something like this:

val action: DBIOAction[Int, NoStream, Effect] =
  for {
    id <- sql"select id from product where name = $name".as[Int].head

  } yield {
     if (id != 5) {sql"update product set description = ${description(id, name)}".asUpdate }
     id
}
Jose Cabrera Zuniga
  • 2,348
  • 3
  • 31
  • 56
  • This does not work, because executing `db.run(action)` will not execute the second sql statement. That is, the second sql statement is totally lost and useless. – David Portabella Dec 18 '19 at 20:11
  • Then, you might have to use slick models... as the for loop you first created suffers from the same problem, isn't it? Suggestion: Look at: https://stackoverflow.com/questions/31443505/slick-3-0-insert-and-then-get-auto-increment-value – Jose Cabrera Zuniga Dec 18 '19 at 22:58