1

I found many old posts on how the "old" AutoInc function works, but there is almost no post on how the new AutoInc function actually works.

https://github.com/slick/slick-examples/blob/master/src/main/scala/com/typesafe/slick/examples/lifted/MultiDBCakeExample.scala

There are two private AutoInc functions defined with User and Picture:

private val picturesAutoInc = pictures 
    returning pictures.map(_.id) into { case (p, id) => p.copy(id = id) }

private val usersAutoInc = users.map(u => (u.name, u.pictureId)) 
    returning users.map(_.id) into {
    case (_, id) => id
}

I found the returning method on http://slick.typesafe.com/doc/2.0.0/queries.html#inserting

But what is this into function? What does it do? What does it take into?

This is my class and how should I write my own autoInc?

  case class Label (id: Option[Int] = None, tag_name: String)

  class Labels (tag: Tag) extends Table[Label](tag, "Labels") {
    def id = column[Option[Int]]("TAG_ID", O.PrimaryKey, O.AutoInc)
    def tag_name = column[String]("TAG_NAME")

    def * = (id, tag_name) <> (Label.tupled, Label.unapply _)
  }
windweller
  • 2,365
  • 5
  • 33
  • 56

1 Answers1

3

It allows you to map the inserted values and generated keys into a desired target value.

It was undocumented up until now. I created a PR with documentation: https://github.com/slick/slick/pull/687

Also note the corresponding unit test: https://github.com/slick/slick/blob/master/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/InsertTest.scala#L59

cvogt
  • 11,260
  • 30
  • 46
  • If the `case (p, id)` is the inserted value, what should we do if we are inserting like more than 10 values for a big table?? – windweller Feb 23 '14 at 01:13
  • 1
    I am not sure you understand your question. The first argument of the function you give to `into` is the value you gave slick for insertion. If you call `someTable.insert(a,b,c)` it is actually auto-tupled to `someTable.insert((a,b,c))`, which means, that you get (a,b,c) as the value. Hope this answers your question. – cvogt Feb 23 '14 at 18:01