Can someone tell me what the best practice is for inserting records with slick 3.x?
I want a simple pattern of insert a record, and the response should be the model with the updating primary key value.
case class User(int: Int, name: String, email: String)
I want to do the following:
- Insert a new record
- Return the model (User) with the updating PK value for the id property
- Throw an exception if the insert failed, which I believe is when the insert returns less than 0 right?
I am using postgresql if that matters.
The docs have this:
val userWithId =
(users returning users.map(_.id)
into ((user,id) => user.copy(id=Some(id)))
) += User(None, "Stefan", "Zeiger")
Is there a helper function that I could use in my entire DB layer that will also return an exception if the insert failed? i.e. if it is successful, return the user with Id otherwise throw an exception.