Slick:如果更新了零行,如何 return 更新错误
Slick: how to return an error on update if zero row was updated
如果我尝试更新 Slick 中不存在的行,我想 return 出错。
我的查询:
userPromotions.filter(promo =>
promo.code === code).update(myUpdatedRow)
我尝试执行的查询(我有一个错误原因 "rowToUpdate.exist" 是一个 Rep[Boolean] 而不是一个 Boolean):
val rowToUpdate = userPromotions.filter(promo => promo.code === code)
if(rowToUpdate.exist) rowToUpdate.update(myUpdatedRow)
else NotFound
有没有办法在单个查询中做到这一点?
如果您使用的是 Slick 3.0 及以上版本
使用DBIO.failed
当更新行数为 0 时您可以使事务失败
userPromotions.filter(_.code === code).update(myUpdatedRow).flatMap { updatedRows =>
if (updatedRows == 0) DBIO.failed(new Exception("0 rows updated"))
else DBIO.successful(updatedRows)
}.transactionally
如果我尝试更新 Slick 中不存在的行,我想 return 出错。
我的查询:
userPromotions.filter(promo =>
promo.code === code).update(myUpdatedRow)
我尝试执行的查询(我有一个错误原因 "rowToUpdate.exist" 是一个 Rep[Boolean] 而不是一个 Boolean):
val rowToUpdate = userPromotions.filter(promo => promo.code === code)
if(rowToUpdate.exist) rowToUpdate.update(myUpdatedRow)
else NotFound
有没有办法在单个查询中做到这一点?
如果您使用的是 Slick 3.0 及以上版本
使用DBIO.failed
当更新行数为 0 时您可以使事务失败
userPromotions.filter(_.code === code).update(myUpdatedRow).flatMap { updatedRows =>
if (updatedRows == 0) DBIO.failed(new Exception("0 rows updated"))
else DBIO.successful(updatedRows)
}.transactionally