Key :phase not found when managing has_one relationship

Hi,

I have a somewhat unexpected error when trying to manage a has_one relationship that is defined between a Session (source) and a Summary (target) - each Session has at most one summary.

session |> Ash.Changeset.manage_relationship(:summary, %{}, on_missing: :destroy)
** (KeyError) key :phase not found in: #FiveWhys.Understand.Session<
...
>

I tried various other combinations of parameters (e.g., type: delete, providing an explicit %{id: summary_id} in the input argument, etc., and every time I get this cryptic key :phase not found. I assume it’s some internal parameter because :phase is not present in any of my attributes.

I read and re-read the help page for manage_relationship/4 many times, and I feel like it’s the proper way to delete a has_one relationship, but of course I might be using it incorrectly. In which case any suggestion on how to destroy the summary would be appreciated.

Note: I can of course go the long route, e.g.,

session = session |> Ash.load!(:summary)
session.summary |> Ash.Changeset.for_destroy(:destroy) |>  Ash.destroy!()

and it works, but I would ideally like to define an :update action on Session to destroy the summary, to make it available via GraphQL etc.

I would appreciate any advice on how to do it best.

Victor

The first argument to manage_relationship is a changeset. Could you show me how you’re trying to set up your update action? If you wanted to make an update action that removes the related thing it would look something like this:

update :remove_related do
  require_atomic? false
  change fn changeset, _ ->
    Ash.Changeset.manage_relationship(changeset, :related, %{}, on_missing: :destroy)
  end
end
1 Like

Ups, I completely missed that the first argument for manage_relationship should be a changeset. Your sample code works, thank you!