Using manage_relationship to delete a related record
I have an action that accepts the ID of a related record. It should delete that record. However, when I tried using the
type: :remove
option, Ash responds with Invalid value provided for notes: changes would create a new related record.
Is this the right approach? Or should I manually delete the record?6 Replies
Here's the action definition:
Here's the referenced
delete_note()
function:
Something does seem strange there...
I don't think
type: :remove
will do the thing as that will attempt to "unrelate" the two things
You might try manage_relationship(...., on_match: {:destroy, :destroy_action_name})
This works:
It successfully deletes the related record.
This is a
Member
has_many Notes
relationship.
I set on_match: {:destroy, :destroy}
.
I also did it to the original code:
But it still doesn't delete the related record.
I ran an IO.inspect
at the end of the function above, and it looks exactly like the IO.inspect
I applied when calling manage_relationship
directly.
They both contain this piece of data:
Hard to follow the specifics. If you could reproduce the behavior in a test that would help a lot!
Here's the test:
Here's the action:
Calling
manage_relationship
doesn't delete the note either:
Okay, I think this has something to do with with converting between string and integer IDs
In the last test I posted, if I change this line,
note_attrs = %{id: to_string(orig_note.id)}
to
note_attrs = %{id: orig_note.id}
,
it passes.
That was it.
I modified my action to this:
I cast the string :id
argument to an integer before calling manage_relationship
.
However, there seems to be the opposite issue when calling manage_relationship
with type: :append
:
Here, the note doesn't get updated when note_attrs.id
is an integer. The note only updates when it's a string.So what likely needs to happen is that we need to cast values to the proper type and use
Ash.Type.equal?
Can you open an issue for this? It should be a relatively mechanical change.
I think people haven’t encountered this before due to most ash users using UUIDs. We should of course fix it just thinking about how it could possibly have been broken for so long