Slick 3.0: Delete rows from multiple tables in a transaction

旧街凉风 提交于 2019-12-06 17:09:20

问题


I want to delete rows from a few tables. My exact intention is depicted in the pseudo SQL statements below,

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 table has 2 columns users_oid and login_infos_oid and joins users and login_infos tables . How do I this nicely in Slick 3.x within a transaction? Thanks.


回答1:


I do not think this is a good solution but for the time being this is what it is,

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)


来源:https://stackoverflow.com/questions/32528816/slick-3-0-delete-rows-from-multiple-tables-in-a-transaction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!