Search code examples
scalaslick

Catch Unique Key Exception Scala Slick


Below is the code I use in inserting into the db however when the name of a group which is unique is entered I get a unique key violation in the logs

override def create(groups: GroupEntity): Future[GroupEntity] = db.run{groupsTableQuery returning groupsTableQuery += groups}

Solution

  • Thanks for the update however I found a simpler way to solve the problem and this was solved in my route. below is the solution

    val saved : Future[GroupEntity] = groupRepositoryImpl.create(group) 
    onComplete(saved){
    case Success(value) => complete(saved.map(_.toJson))
    case Failure(ex)    => complete((InternalServerError, s"An error occurred: ${ex.getMessage}"))}
    

    result => An error occurred: ERROR: duplicate key value violates unique constraint "groups_name_key_name " Detail: Key (groups_name)=(Test Shop) already exists.