How to create has_one relationships in a nested form
I have a resource called
Transaction
that has a has_one
relationship with a resource called RecurrencePattern
. When creating a new transaction (usually through a form in LV) I want to also create a matching recurrence_pattern. This works great when using the create
action directly, but I'm getting an error when using an AshPhoenix.Form
(I believe I got it to work at some point but don't know how to get back there again π
)
This works:
This gives me an error:
The error inside the changeset:
23 Replies
This is the action
I have tried types
create
and direct_control
So on your recurrence pattern, you probably have a primary create action with a required argument of
transaction_id
.
If that is the case, what you need to do is 1. make that argument optional, 2. use a different create action in your manage relationshipIn recurrence pattern I'm using the default create but based on what you say I suspect the issue could be in the relationship + primary key.
I did try setting
allow_nil?
to true, which Ash obviously didn't like ππ€
I guess the missing
transaction_id
is just failing the form validation but the action can do it's job fine. When I add a random transaction_id
to the recurrence pattern form it actually works and even sets the correct id
π€π€π€π€π€
This is a pretty standard pattern so im not sure why its not working for you
It did work last week and stopped working. I changed a lot of stuff so not sure where I broke it. I believe maybe after upgrading Phoenix, LV/Surface and Ash to the latest version
Interesting⦠could you try the version of ash that you were on before? Would be great to confirm that it broke somewhere down the line.
Tried downgrading to these versions and getting the same error there
It looks like the issue really is setting the
transaction_id
as the primary key (and therefore also allow_nil? false
)
If I add integer_primary_key :id
to the attributes and remove primary_key? true
and allow_nil? false
from the transaction relationship the form passes validation and correctly saves all resources.
I'd like to keep the table structure as it is if possible, is there a way to tell the form/changeset to not validate the transaction_id
?
I could also use the :create
action directly, but that doesn't sound right.I think this is something we need to fix on our end
Basically that required attribute would naturally not be set when creating because the value doesnβt exist yet. So ash forms need to ignore that error.
@miguels can you try
main
for me?Hi @Zach Daniel thanks for the update
Tried with
{:ash, github: "ash-project/ash", branch: "main", override: true}
but still getting the same error.
Oh but if I remove attribute_writable? true
from the relationship it works!
Tried it because I spotted private_and_belongs_to?
in the commit.
I might have been wasting your time π
Downgraded back to {:ash, "~> 2.7"}
and also tried removing attribute_writable? true
from the relationship and it works.Well, it should work with
attribute_writable? true
Oh, yeah I see why my fix didnβt work
okay can you try main again?Still seeing the error on the main branch with
attribute_writable? true
π₯²π₯²π₯²
Ah, actually I know why
okay,
ash
main and ash_phoenix
main π
LMK if that worksSorry still seeing the same error
okay, okay, okay. Going to try one more thing. I'm actually technically on vacation which is why I've been ducking in and out trying to fix this instead of taking my time π sorry about that
lemme runs ome checks and then I'll push it up
okay update
ash_phoenix
main againNow I feel bad for making you work during your vacation π
no worries, you didn't make me π
I just know this issue will affect other users as well so I want to get it handled π
Tests are passing π
:Fire:
excellent
I'll release new versions of those packages soon
Thanks a lot for taking the time
This also taught me things I didn't know about Ash
π₯³ glad to be of help.