Slick 3.0:从事务中的多个表中删除行

时间:2015-09-11 17:16:27

标签: scala slick

我想从几个表中删除行。我的确切意图在下面的伪SQL语句中描述,

delete from users where oid={user_oid};
login_infos_oid = select login_infos_oid from users_login_infos where users_oid={user_oid};
delete from users_login_infos where users_oid={user_oid};
delete from password_infos where login_infos_oid={login_infos_oid};
delete from login_infos where oid={login_infos_oid};

users_login_infos表有2列users_oidlogin_infos_oid,并且加入userslogin_infos表。在交易中,如何在Slick 3.x中很好地解决这个问题?感谢。

1 个答案:

答案 0 :(得分:2)

我认为这不是一个好的解决方案,但目前就是这样,

val userQuery = slickUsers.filter(_.username === username)
val userLoginInfoQuery = slickUserLoginInfos.filter(_.userOid in userQuery.map(_.oid))
val loginInfoQuery = slickLoginInfos.filter(_.oid in userLoginInfoQuery.map(_.loginInfoOid))
val passwordInfoQuery = slickPasswordInfos.filter(_.loginInfoOID in userLoginInfoQuery.map(_.loginInfoOid))

db.run((loginInfoQuery.delete andThen
  passwordInfoQuery.delete andThen
  userLoginInfoQuery.delete andThen
  userQuery.delete).transactionally)