Problem: The functions `Repo.reload/2` (and its associated bang function) do not support reloading items with multiple primary keys.
Current solution: Here are the helper functions I have created to accomplish this in some of my tests:
```elixir
defmodule MyProject.TestHelpers.Ecto.Repo do
@moduledoc "`Ecto.Repo` test helpers."
alias MyProject.Repo
@doc "Similar to `Repo.reload/2`, but supports structs with multiple primary keys."
def reload(%_{} = struct, opts \\ []) do
case struct.__struct__.__schema__(:primary_key) do
[_pk] ->
Repo.reload(struct, opts)
_pks ->
get_by_clauses =
struct.__struct__.__schema__(:primary_key)
|> Keyword.new(fn primary_key_field ->
{primary_key_field, Map.fetch!(struct, primary_key_field)}
end)
Repo.get_by(struct.__struct__, get_by_clauses, opts)
end
end
@doc "Similar to `Repo.reload!/2`, but supports structs with multiple primary keys."
def reload!(%_{} = struct, opts \\ []) do
case struct.__struct__.__schema__(:primary_key) do
[_pk] ->
Repo.reload!(struct, opts)
_pks ->
get_by_clauses =
struct.__struct__.__schema__(:primary_key)
|> Keyword.new(fn primary_key_field ->
{primary_key_field, Map.fetch!(struct, primary_key_field)}
end)
Repo.get_by!(struct.__struct__, get_by_clauses, opts)
end
end
end
```
Proposal: Is there any interest in integrating this functionality into Ecto?
I realize that I would have to modify the functions to use the abstractions used by the existing
Repo.reload functions. I just wanted to reach out before doing any work in case the omission of this functionality is intentional.
Thanks