使用 Ecto 删除孤立记录

Delete orphaned records with Ecto

我有 3 个模型:

user.ex

  schema "users" do 
    ...
    many_to_many(:acts, Act, join_through: UserAct)
  end

act.ex

  schema "acts" do
    ...
    many_to_many(:users, User, join_through: UserAct)
  end

user_act.ex

  schema "users_acts" do
    belongs_to :user, User
    belongs_to :act, Act
  end

每次我删除 UserAct 我想检查是否有孤立的 Act 模型并在事务中删除它们。

在SQL中看起来像这样

DELETE FROM acts WHERE NOT EXISTS (
  SELECT 1 FROM users_acts ua WHERE ua.act_id = acts.id
);

or 

DELETE FROM acts WHERE id NOT IN (SELECT act_id FROM users_acts);

我的问题是如何使用 Ecto 编写类似的查询?

请展示您知道的所有方法:连接、片段等...

一种解决方案是使用片段。

Repo.delete_all(
  from a in Act,
  where: fragment("? NOT IN (SELECT act_id FROM users_acts)", a.id)
)

使用联接:

orphaned_query = Act
|> join(:left, [a], a in assoc(a, :users))
|> where([a, u], is_nil(u.id))

Act
|> join(:inner, [a], a in subquery(orphaned_query))
|> Repo.delete_all()