Update multiple columns

1,975 views
Skip to first unread message

Program Files

unread,
Feb 2, 2013, 6:57:33 PM2/2/13
to scala...@googlegroups.com
It seems that I can update either single column or entire row

Given following table and model class:
case class Post(id: Option[Long], version:Int, title: String, content: String)

object Posts extends Table[Post]("posts") {
  def id = column[Long]("id", O.PrimaryKey)
  def version = column[Int]("version")
  def title = column[String]("title")
  def content = column[String]("content")
  def * = id.? ~ version ~ title ~ content <>(Post, Post.unapply _)
}

I'm trying to update it:

      val selectEntirePost:scala.slick.lifted.Query[Table[Post], Post] = 
        (for {post <- Posts if post.id === 1L} yield (post))
      selectEntirePost.update(new Post(Some(1), 1, "hello", "world")) //works

      val selectSingleColumn:scala.slick.lifted.Query[Column[String], String] =
              (for {post <- Posts if post.id === 1L} yield ( post.content))
      selectSingleColumn.update("big world") //works

      val selectMultipleColumns:scala.slick.lifted.Query[(Column[Int], Column[String]), (Int, String)] =
        (for {post <- Posts if post.id === 1L} yield (post.version, post.content))
      selectMultipleColumns.update(2, "cruel world") // value update is not a member of scala.slick.lifted.Query[(slick.driver.H2Driver.simple.Column[Int], slick.driver.H2Driver.simple.Column[String]),(Int, String)]

Is it a bug?

ijuma

unread,
Feb 3, 2013, 9:07:55 AM2/3/13
to scala...@googlegroups.com
It is definitely possible to update multiple columns by calling `map` before `update`. I have a number of examples in my project that are like:

        Query(FooTable).where(...).map { r =>
          r.col1 ~ r.col2
        }.update((newCol1, newCol2))

Not sure what is wrong with your examples, but I'd start by letting scalac infer the type of the query instances instead of setting them yourself.

Best,
Ismael

Program Files

unread,
Feb 4, 2013, 8:40:32 AM2/4/13
to scala...@googlegroups.com
> Not sure what is wrong with your examples, but I'd start by letting scalac infer the type of the query instances instead of setting them yourself.
Intellij Idea type aware highlighter complains that there is no 'update' method unless types are declared explicitly.

(for {post <- Posts if post.id === 1L} yield (post.version, post.content)).update(...) // doesn't work
(for {post <- Posts if post.id === 1L} yield (post.version ~ post.content)).update(...) //works
Reply all
Reply to author
Forward
0 new messages