Ash FrameworkAF
Ash Frameworkโ€ข3y agoโ€ข
7 replies
Terryble

many_to_many exception after updating Ash

Previous version was 2.6.31. I updated to 2.11.8 today and an existing many_to_many association that I had is now raising an exception. Did something change?

Here are my resources:

defmodule MyApp.Budgeting.Book do
  relationships do
    many_to_many :users, MyApp.Authentication.User do
      through MyApp.Budgeting.BookUser
      api MyApp.Authentication
      source_attribute_on_join_resource :book_id
      destination_attribute_on_join_resource :user_id
    end
  end
end


defmodule MyApp.Authentication.User do
  relationships do
    many_to_many :books, MyApp.Budgeting.Book do
      through MyApp.Budgeting.BookUser
      api MyApp.Budgeting
      source_attribute_on_join_resource :user_id
      destination_attribute_on_join_resource :book_id
    end
  end
end


defmodule MyApp.Budgeting.BookUser do
  relationships do
    belongs_to :book, MyApp.Budgeting.Book do
      allow_nil? false
    end

    belongs_to :user, MyApp.Authentication.User do
      api MyApp.Authentication
      allow_nil? false
    end
  end
end


Here's the error I'm getting:

** (RuntimeError) Resource `MyApp.Budgeting.BookUser` is not accepted by api `MyApp.Authentication` for autogenerated join relationship: `:users_join_assoc`

Relationship was generated by the `many_to_many` relationship `:users`

If the `through` resource `MyApp.Budgeting.BookUser` is not accepted by the same
api as the destination resource `MyApp.Authentication.User`,
then you must define that relationship manually. To define it manually, add the following to your
relationships:

    has_many :users_join_assoc, MyApp.Budgeting.BookUser do
      # configure the relationship attributes
      ...
    end

You can use a name other than `:users_join_assoc`, but if you do, make sure to
add that to `:users`, i.e

    many_to_many :users_join_assoc, MyApp.Budgeting.BookUser do
      ...
      join_relationship_name :your_new_name
    end
Was this page helpful?