AE
Ash Elixirโ€ข3y ago
gvanders

Auto forms silently drops some associations

I ran into this while trying to use auto forms, grabbing values from the created _join form. There were no errors, but not all of the resource associations were present. Looks like here https://github.com/ash-project/ash_phoenix/blob/main/lib/ash_phoenix/form/auto.ex#L469 we use Enum.find, so we are only getting the first occurence of the association. I tried editing it to Enum.filterand changing the type from :single to :list. This made the associations properly appear in the form, but submitting it failed. Got me wondering a few things. First, maybe I screwed up somewhere before ๐Ÿ˜… . Secondly for fixing it if it is a bug, it doesn't seem like we can know :single or :list beforehand , so would we need to use an anonymous function to resolve which similar to how data is resolved?
type: :single,
data: &get_join(&1, &2, relationship),
type: :single,
data: &get_join(&1, &2, relationship),
Alternatively, is it possible to just make them all :list and handle the length = 1 case for single?
GitHub
ash_phoenix/auto.ex at main ยท ash-project/ash_phoenix
https://hexdocs.pm/ash_phoenix. Contribute to ash-project/ash_phoenix development by creating an account on GitHub.
14 Replies
ZachDaniel
ZachDanielโ€ข3y ago
๐Ÿค” Each related item should only have a single join row though, right? Oh, is it possible to have the same thing related multiple times but with different join attributes?
gvanders
gvandersOPโ€ข3y ago
Yeah exactly, this is self referencing many_to_many, with different join attributes I can probably change how this is modeled but it would be cool to support it if possible. Happy to make the changes if theres a clear path
ZachDaniel
ZachDanielโ€ข3y ago
So you have a join relationship like this:
A -> foo: 1 -> A
A -> foo: 2 -> A
A -> foo: 3 -> B
A -> foo: 1 -> A
A -> foo: 2 -> A
A -> foo: 3 -> B
where the same thing can be related multiple times with a different join row
gvanders
gvandersOPโ€ข3y ago
Yes
ZachDaniel
ZachDanielโ€ข3y ago
Interesting. Makes sense but probably signals something wrong with our implementation there in ash_phoenix I'm not sure exactly what we could do there to figure it out though really. Like its expecting to produce a single form for each related thing (which is produced uniquely), whereas the join will have multiple join rows pointing at the same related record. i.e if you do Api.load(..., :many_to_many) you won't get duplicates based on duplicate rows, and I think its going to be expecting the same thing. I think what you actually want to do with this is use manage_relationship on the join relationship itself, and then a nested manage_relationship on the destination. Auto forms can stitch those together. Then you'd get one row per join in your form.
gvanders
gvandersOPโ€ข3y ago
Api.load(..., :many_to_many) does produce all the rows I believe
ZachDaniel
ZachDanielโ€ข3y ago
๐Ÿค” it duplicates destination records? If it does then that is a bug
gvanders
gvandersOPโ€ข3y ago
Ahhh sorry thats my bad. no it doesn't. Associated resource is not duplicated. Both assocations are present on resource_join
ZachDaniel
ZachDanielโ€ข3y ago
Gotcha, yeah Yeah, I think I'd recommend reworking it to edit it through the join TBH
gvanders
gvandersOPโ€ข3y ago
I'll take a crack at it So for the join relationship in the update action I've got
change(manage_relationship(:source, on_match: :update, on_no_match: :ignore))
change(manage_relationship(:destination, on_match: :update, on_no_match: :ignore))
change(manage_relationship(:source, on_match: :update, on_no_match: :ignore))
change(manage_relationship(:destination, on_match: :update, on_no_match: :ignore))
In the resource, action I have
change(
manage_relationship(:associations,
on_lookup: :relate,
on_match: {:update, :update, :update},
on_missing: {:create, :create, :create}
)
)
change(
manage_relationship(:associations,
on_lookup: :relate,
on_match: {:update, :update, :update},
on_missing: {:create, :create, :create}
)
)
Is this what you mean by nested "manage_relationship". Searching through discord I see some changesets with a relationships: key but its from 2021 and haven't seen it in docs
ZachDaniel
ZachDanielโ€ข3y ago
Yeah that looks right
gvanders
gvandersOPโ€ข3y ago
Same issue as before :/
ZachDaniel
ZachDanielโ€ข3y ago
๐Ÿค” perhaps I didn't understand the original issue then Before submitting the form, take a look at the params being generated at submit and make sure those look rght (or if they are wrnog, perhaps show an example of the wrong params and what it owould look like if they were right)
gvanders
gvandersOPโ€ข3y ago
Unconfused myself and we are in business ๐Ÿ™‚ One positive side effect is also not having to do the [:join_table, :params], restricting number of different patterns to do same thing. Thanks again for all the help

Did you find this page helpful?