Destroying related resources
I have some related resources with existing actions utilizing
manage_relationship
to create and update them. For destroying, is it pretty much the same process, where the form has all the nested subforms and then I take those in through arguments and manage_relationship them and destroy them? I haven't seen anything explicitly on this topic in the documentation or here, pardon me if I've missed it. I want to make sure I don't leave any orphaned resources, which the loaded form on my edit page should take care of, right, but I'm wondering if there's a way you're supposed to do it directly in the action to be sure.8 Replies
if you want to be 100% certain, you can make sure you use Foreign Keys and cascade deletes
I'm ~not familiar with how to instrument that from within Ash.
The setup I have now is
edit.ex
site.ex
And I'm getting the error:
The form appears to have those subforms in it when I
IO.inspect
it, so I'm a bit perplexed. Maybe I'm keying it wrong? It mostly matches the setup for my create and edit actions right now, except without an actual form you can edit in the template itself, since we're just destroying.Ah, yeah so this isn't how you want to do this
Then generate migrations
This will tell the database to destroy those things whenever the parent thing is destroyed
Okay yeah that's way more modelly and Ash-y. I figured there would be something nice like that.
When I drop and recreate the database with migrations and all, it's still telling me the delete can't be performed because it would leave records behind:
Specifically calling out Profiles. Is there more I should be aware of when constructing these relationships? I'm reading through everything I can find on references on ashhq now, thank you!!
That’s the primary thing, configuring the references let’s you do pretty much everything you should need for this case
Okay so it looks like the references are supposed to be defined on each child, not on the parent, so I had that backwards initially. That was my misunderstanding which I corrected by looking through the realworld source here: https://github.com/team-alembic/realworld/search?q=reference
I then defined the references block on each of the child resources of the Site (Domains, Configuration, etc) and now they all tidily happily disappear when their Site is destroyed.
So I think I get it now. Thank you 🥲
GitHub
Search · reference · team-alembic/realworld
Contribute to team-alembic/realworld development by creating an account on GitHub.
Is there a builtin to accomplish the same at the Ash layer, for the case where not all resources are in Postgres?
I don't believe so, no