Using Auto Incrementing fields with PostgreSQL and Slick

前端 未结 7 1941
無奈伤痛
無奈伤痛 2020-12-30 00:43

How does one insert records into PostgreSQL using AutoInc keys with Slick mapped tables? If I use and Option for the id in my case class and set it to None, then PostgreSQL

7条回答
  •  有刺的猬
    2020-12-30 01:27

    The solution I've found is to use SqlType("Serial") in the column definition. I haven't tested it extensively yet, but it seems to work so far.

    So instead of

    def id: Rep[PK[SomeTable]] = column[PK[SomeTable]]("id", O.PrimaryKey, O.AutoInc)
    

    You should do:

    def id: Rep[PK[SomeTable]] = column[PK[SomeTable]]("id", SqlType("SERIAL"), O.PrimaryKey, O.AutoInc)
    

    Where PK is defined like the example in the "Essential Slick" book:

    final case class PK[A](value: Long = 0L) extends AnyVal with MappedTo[Long]
    

提交回复
热议问题