Ecto/Elixir/Phoenix - 获取没有关联记录的记录

问题描述

我有这些架构:

  schema "players" do
    field :email,:string
    
    has_many :profiles,MyApp.Profile
    has_many :worlds,through: [:profiles,:world]
  end
  schema "worlds" do
    field :name,MyApp.Profile
    has_many :players,:player]
  end
  schema "settings" do
    field :mode,:string
    
    belongs_to :player,MyApp.Player
    belongs_to :world,MyApp.World
  end

默认情况下,所有玩家在他们创建的每个世界中都应该有一个设置。但是由于我们代码中的逻辑错误,一些玩家在某些世界中没有设置。

现在我正在尝试找到在某个世界中没有现有 players 记录的那些 settings,以便我可以使用播种机为他们创建默认设置。

我尝试过这样的解决方法:

query = from profile in Profile

query
|> Repo.all()
|> Enum.each(fn profile ->
  case get_settings(profile.player_id,profile.world_id) do
    nil ->
      create_settings(profile.player_id,profile.world_id)

    _ ->
      :ok
  end
end)

它有效,但我想避免使用 case 语句。它花费了大量的数据库工作。 有没有办法使用查询在某些 players 中获取那些没有现有 settings 记录的 worlds

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)