Many to Many Error
Given these relationships:
All of them are in the same API and Registry.
I'm getting an error that I don't quite understand (join_relationship_name???):
I'm probably missing something obvious, but I can't spot it.
15 Replies
I have not gotten many to many to work quite yet, but mine at lest compile. I suggest looking for references or imports or aliases of Hsm.Ash.MCP.StepCapability given it is in the error and not appearly what you are expecting to be involved.
Yeah, pasted the wrong defs. I don't use aliases to avoid those kinds of problems. Post edited w/ correct definitions. ^
Oh, is this as simple as needing to add it to both registries? :thinkies:
It is making up the resource name 'steps_join_assoc' for some reason. It looks like you have through specified, I would double check that the join resource is in the same API and registry as the others.
The join resource is in the
MCP
registry/api, not Workcenters
. I'm just not clear on how to deal with that, as specifying the api on either/both many_to_many
still produces the same error. It's also not clear in the docs if the api option in a many_to_many
is supposed to point to the API of t he destination resource or the join resource.The docs state that the API option is for the destination resource. Looks like it is a missing option. I would file an issue, and in the mean time add the join resource to both API and Registries.
The docs say "related entity" for the api on a many_to_many, which to me is not clearly indicating "destination", which is the term it consistently uses. I mean, that's probably what it means, but it's unclear IMO.
Yeah, I'm consistently doing something wrong w/ my cross-api many-to-many relationships because they all have this problem. @Zach Daniel When you get a chance, could use some guidance. (no rush) 😵💫
OK, I think the solution was a few steps: 1) add the
api
options to all the many-to-many relationships, 2) add the join resource to both api/registries and 3) make sure the join resource specifies the API for each relationship. Need to do this to the rest of them to make sure.You shouldn’t need to add the join resource to both apis
*registries
The idea of the warning is that if you set the api of a many to many relationship, and we generate a has_many for it, we assume that the through resource is in the destination api.
If it’s not, then you need to define the underlying has_many yourself and not set an api (or set it to the correct api)
And then use that with
join_relationship
And the api in a many to many should point to the destination resource api.Might be a bug then, because given this:
Hsm.Ash.MCP.StepCapability
is added to the MCP
registry, but will still give the compile error unless I also add it to the Workcenters
registry. :thinkies:Well, the through resource is only in one of them
So you will always get an error unless you manually define the underlying
has_many
relationship for one of them
not specifying the api for that one (I.e the through resource is in my api not theirs)
If you define two many to many relationships across an api boundary without doing that, one of them will always error unless the through resource is in both.Ahh... OK. Is there any harm to just including the through relationship in both registries to avoid the manual
has_many
? Just trying to figure out best practice, will PR some of these details in the guides/docs.No harm really, no.
But I’d suggest just picking which api it belongs in and on one end define the has_many for the many_to_many
came up against this now, it doesn't seem to be documented in the relationship docs anywhere
actually I'm really confused by what I'm supposed to do here. I'm getting:
am I supposed to literally call it
:users_join_assoc
?
I think having an example of this in the docs would be really helpful, right now the docs only says that the api
must be defined if many_to_many
is crossing api boundariesYou can call it whatever you want, but then you need to say
join_relationship :relationship_name
in your many to many.
Agreed an example would be helpfulI have a question related to the many_to_many. I have a pretty basic many to many in one Api. Currently I'm seeding some data, but it fails to write the join table when I add the associated data directly in the params. Is this not supported (yet)? In Ecto this would mean adding the cast_assoc for the related items
checking out the managing relationships guide as it seems that's where I can configure this
Yep 🙂 managing relationships (or custom changes w/ after action hooks) is what you'd want there