`many_to_many` error

** (RuntimeError) Resource `SwiftEco.Resources.Accounts.UsersCompanies` is not in registry `SwiftEco.Resources.Companies` for autogenerated join relationship: `:users_join_assoc`

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

If the `through` resource `SwiftEco.Resources.Accounts.UsersCompanies` is not accepted by the same
api as the destination resource `SwiftEco.Resources.Accounts.User`,
then you must define that relationship manually. To define it manually, add the following to your
relationships:

has_many :users_join_assoc, SwiftEco.Resources.Accounts.UsersCompanies 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, SwiftEco.Resources.Accounts.UsersCompanies do
...
join_relationship_name :your_new_name
end
** (RuntimeError) Resource `SwiftEco.Resources.Accounts.UsersCompanies` is not in registry `SwiftEco.Resources.Companies` for autogenerated join relationship: `:users_join_assoc`

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

If the `through` resource `SwiftEco.Resources.Accounts.UsersCompanies` is not accepted by the same
api as the destination resource `SwiftEco.Resources.Accounts.User`,
then you must define that relationship manually. To define it manually, add the following to your
relationships:

has_many :users_join_assoc, SwiftEco.Resources.Accounts.UsersCompanies 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, SwiftEco.Resources.Accounts.UsersCompanies do
...
join_relationship_name :your_new_name
end
Happy to share any code needed just not sure what the actual error means! It was working before and I have just started work on tidying up the codebase and this is now an error that has appeared
5 Replies
harry
harryOP3y ago
Looks like this is an error with all resources that have relationships across API (registries?) after updating ash Okay downgraded and it works again but would like to upgrade any advice how to migrate from 2.9 from 2.5
ZachDaniel
ZachDaniel3y ago
The error contains instructions on how to fix it This was a new validation that was added to prevent other bugs from popping up (which they would have). If you have a many to many across APIs you have to consider what api the through resource is in, and possibly define the underlying has_many relationship yourself.
harry
harryOP3y ago
So I need to make the has_many for companies and users on the join?
ZachDaniel
ZachDaniel3y ago
Yeah so the problem is that, for the many_to_many you have the api set, but the join resource is not in that api. It’s in the source api. So has_many :users_companies, UserCompany And then in your many to many join_relationship :users_companies
axdc
axdc3y ago
If it helps, this is a current cross-api many_to_many that I got working with some help here: MyApp.Sites.Site is the source, MyApp.Accounts.User is the destination, MyApp.Sites.SitesUsers is the join.
relationships do
# https://discord.com/channels/711271361523351632/1099572167638794290/1100294369153658930
has_many :sites_users, MyApp.Sites.SitesUsers do
api MyApp.Sites
end

# https://discord.com/channels/711271361523351632/1074712810505908254
many_to_many :users, MyApp.Accounts.User do
api(MyApp.Accounts)
through(MyApp.Sites.SitesUsers)
source_attribute(:id)
source_attribute_on_join_resource(:site_id)
destination_attribute(:id)
destination_attribute_on_join_resource(:user_id)
join_relationship :sites_users
end
end
relationships do
# https://discord.com/channels/711271361523351632/1099572167638794290/1100294369153658930
has_many :sites_users, MyApp.Sites.SitesUsers do
api MyApp.Sites
end

# https://discord.com/channels/711271361523351632/1074712810505908254
many_to_many :users, MyApp.Accounts.User do
api(MyApp.Accounts)
through(MyApp.Sites.SitesUsers)
source_attribute(:id)
source_attribute_on_join_resource(:site_id)
destination_attribute(:id)
destination_attribute_on_join_resource(:user_id)
join_relationship :sites_users
end
end
Just had to specify where everything comes from and points to to help Ash hook it all up for me 😄

Did you find this page helpful?