Ash FrameworkAF
Ash Framework2mo ago
12 replies
Chris O'Donnell

Confusing error from manage_relationship

I have the following many-to-many relationship:

# Team
defmodule LittleRetro.Organizations.Team do
  # ... normal resource/postgres stuff

  actions do
    create :create do
      primary? true
      accept [:name]

      change LittleRetro.Organizations.Changes.AssignActorTeamAdmin
    end

    defaults [:read]
  end

  attributes do
    uuid_v7_primary_key :id

    attribute :name, :string do
      allow_nil? false
    end
  end

  relationships do
    has_many :team_members, LittleRetro.Organizations.TeamMember
  end
end

# TeamMember
defmodule LittleRetro.Organizations.TeamMember do
  # ... normal resource/postgres stuff

  actions do
    defaults [:create, :read, :destroy, :update]
  end

  attributes do
    uuid_v7_primary_key :id

    attribute :role, LittleRetro.Organizations.TeamMemberRole do
      allow_nil? false
    end
  end

  relationships do
    belongs_to :user, LittleRetro.Accounts.User do
      allow_nil? false
    end

    belongs_to :team, LittleRetro.Organizations.Team do
      allow_nil? false
    end
  end

  identities do
    identity :unique_membership, [:user_id, :team_id]
  end
end

# Change
defmodule LittleRetro.Organizations.Changes.AssignActorTeamAdmin do
  use Ash.Resource.Change

  def change(changeset, _opts, context) do
    current_user = context.actor

    members = [%{user_id: current_user.id, role: :admin}]

    Ash.Changeset.manage_relationship(
      changeset,
      :team_members,
      members,
      type: :direct_control,
      debug?: true
    )
  end
end


However, when I try to run LittleRetro.Organizations.create_team!(%{name: name}, actor: user) I get the following error:

Invalid Error

     * attribute role is required
         at team_members, 0


I'm a bit confused, since I have included a :role key in the map I'm passing to manage_relationship. Also, including the debug?: true option doesn't appear to log anything even with log level set to debug. Any help would be appreciated!
Was this page helpful?