如果不存在,我试图插入,如果存在则获取该行。我想出了这个:
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
编辑
进行一些修改后,现在我得到:
java.util.NoSuchElementException: Action.withFilter failed
问候