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.filter
and 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?
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
๐ค 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?
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
So you have a join relationship like this:
where the same thing can be related multiple times with a different join row
Yes
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.Api.load(..., :many_to_many) does produce all the rows I believe
๐ค it duplicates destination records? If it does then that is a bug
Ahhh sorry thats my bad. no it doesn't. Associated resource is not duplicated. Both assocations are present on resource_join
Gotcha, yeah
Yeah, I think I'd recommend reworking it to edit it through the join TBH
I'll take a crack at it
So for the join relationship in the
update action
I've got
In the resource, action I have
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 docsYeah that looks right
Same issue as before :/
๐ค 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)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