Ash FrameworkAF
Ash Framework2mo ago
6 replies
Aaric

Is it possible to use order_is_key when managing a many_to_many relationship?

I have the following resources.

defmodule App.Catalog.Book do
  actions do
    create :create do
      primary? true

      argument :authors, {:array, :uuid} do
        allow_nil? false
      end

      change manage_relationship(:authors, type: :direct_control)
    end
  end

  relationships do
    has_many :authorships, App.Catalog.Authorship, sort: [order: :asc]

    many_to_many :authors, App.Catalog.UserProfile, join_relationship: :authorships, destination_attribute_on_join_resource: :author_id
  end
end

defmodule App.Catalog.Authorship do
  attributes do
    attribute :order, :integer, allow_nil?: false
  end

  relationships do
    belongs_to :resource, App.Catalog.Resource do
      primary_key? true
      allow_nil? false
    end

    belongs_to :author, App.Catalog.UserProfile do
      primary_key? true
      allow_nil? false
    end
  end
end

Is it possible to have the change manage_relationship(:authors, type: :direct_control) set the order attribute on the join resource?

I tried using order_is_key: :order but got an invalid error attribute order is required.
Was this page helpful?