Using Auto Incrementing fields with PostgreSQL and Slick

前端 未结 7 1974
無奈伤痛
無奈伤痛 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:38

    This is working here:

    object Application extends Table[(Long, String)]("application") {   
        def idlApplication = column[Long]("idlapplication", O.PrimaryKey, O.AutoInc)
        def appName = column[String]("appname")
        def * = idlApplication ~ appName
        def autoInc = appName returning idlApplication
    }
    
    var id = Application.autoInc.insert("App1")
    

    This is how my SQL looks:

    CREATE TABLE application
    (idlapplication BIGSERIAL PRIMARY KEY,
    appName VARCHAR(500));
    

    Update:

    The specific problem with regard to a mapped table with User (as in the question) can be solved as follows:

      def forInsert = first ~ last <>
        ({ (f, l) => User(None, f, l) }, { u:User => Some((u.first, u.last)) })
    

    This is from the test cases in the Slick git repository.

提交回复
热议问题