2023-01-25

How to return object with scala slick after insert

I'm trying to use generic insert function that would return object when inserting data using Slick. I've tried with following code

        // This works without any issues but limited to specific entity and table
    def insertEmployee(employee: Employee): IO[Employee] = DB.run(db, {
        def returnObj = (EmployeeTable.instance returning EmployeeTable.instance.map(_.id)).into((obj, id) => obj.copy(id = id))
        returnObj += employee
    })
    
    // This doesn't work and returns error on "copy". Error: Cannot resolve symbol copy
    def withReturningObj[E <: BaseEntity, T <: BaseTable[E]](query: TableQuery[T]) =
        (query returning query.map(_.id)).into((obj, id) => obj.copy(id = id))

Anyone who could suggest a possible solution?



No comments:

Post a Comment