scala - Insert or get in Slick 3 -
i trying insert if not exists, , row if does. come this:
def saveorget(u: user) = (for { userget <- get(u.name).map(r => (r.id, r.active)).result.headoption id <- save(u) if userget.isempty } yield { userget match { case some((id, active)) => (user(some(id), u.name, active), false) case none => (user(some(id), u.name, u.active), true) } }).transactionally private def get(name: string) = users.filter(_.name === name).take(1) private def save(u: user) = users returning users.map(_.id) += u
edit
after making modifications, get:
java.util.nosuchelementexception: action.withfilter failed
regards
try using case _
, because seems it's possible have empty rather none
userget match { case some((id, active)) => (user(some(id), u.name, active), false) case _ => (user(some(id), u.name, u.active), true) }
Comments
Post a Comment