Hi everyone, I’m running into this error I absolutely can’t make sense of while working with Ecto.Multi (I’m relatively new but feel like I have a decent grasp) and so far Google searches haven’t yielded anything.
Here is the error:
expected Ecto.Multi callback named `:relationship` to return either {:ok, value} or {:error, value}, got: {:error, :relationship, "not found", %{}}
Now I am testing a failure case, but I am expecting an error to get passed through so that the view can display an error instead of an exception. The weird thing is I have inspected the value returned from the anonymous function in Multi.run and the value is {:error, :not_found}
which based on this error message I think should be fine?
Here is the function that is running into this error:
def update_relationship(%SocialUser{} = user_a, %SocialUser{} = user_b, %{} = info_attrs, with_changeset) do
Multi.new()
|> Multi.run(:relationship, fn _repo, _changes -> get_relationship(user_a, user_b) end)
|> Multi.update(:update, fn %{relationship: %{info: info}} -> with_changeset.(info, info_attrs) end)
|> Repo.transaction()
end
Here is the code for get_relationship/2
:
def get_relationship(%SocialUser{} = user_a, %SocialUser{} = user_b) do
Repo.get_one(from r in Relationship, where: r.user_a_id == ^user_a.id and r.user_b_id == ^user_b.id, preload: [:info])
end
And finally Repo.get_one
is a function I’ve written to make it easier to work with Repo.one
in situations like this where you need a tuple:
def get_one(query) do
case one(query) do
nil -> { :error, :not_found }
row -> { :ok, row }
end
end
Am I doing something stupid? I can’t for the life of me figure out where this transformation of {:error, :not_found}
to {:error, :relationship, "not found", %{}}
is happening, as far as I can tell I’m following what the documentation says