Getting autoincrement values with Slick library in Scala

前端 未结 4 545
醉酒成梦
醉酒成梦 2021-02-01 21:22

How do I get the auto-incremented values for records inserted with Slick? The following code prints 1111. I would have expected it to print 1234

import scala.sli         


        
4条回答
  •  -上瘾入骨i
    2021-02-01 22:12

    According to SLICK Inserting document, you should define a method forInsert looks like:

    def forInsert = first ~ last <> (
        { t =>
            User(None, t._1, t._2)},
        { (u: User) =>
            Some((u.first, u.last))
        }) returning id
    

    To get result:

    def save(user: User): User = {
        val id = users.forInsert.insert(user)
        new User(Some(id), user.first, user.last)
    }
    

提交回复
热议问题