Using manage_relationship with bulk_create
I'm having trouble using
manage_relationship
with a bulk create. I want to be able to create a bunch of "child" resources in one request while also providing attributes to create their "parent" resources at the same time. In a normal create request, I can create a child and parent at the same time, but in a bulk create, it seems like it's trying to insert the parent resource twice.
Child resource:
Parent resource:
Bulk create:
When I try the above bulk_create, I get an Ash.Error.Invalid error: Invalid value provided for id: has already been taken.
I created a reproduction test in a toy project: https://github.com/averypeck/helpdesk/blob/81eb8e9ab6e12454d3294cc925c5f51b24063a55/test/helpdesk_test.exs#L45-L55GitHub
helpdesk/test/helpdesk_test.exs at 81eb8e9ab6e12454d3294cc925c5f51b...
Contribute to averypeck/helpdesk development by creating an account on GitHub.
6 Replies
The
type: :create
means that each one will try to create their parent
You'd want to do something to prevent the thing from being created on future calls.
Like maybe using the on_lookup
option, or creating the element in advance.The type: :create means that each one will try to create their parent
this is my intention. I guess my issue is that within the one bulk_create
call, there appear to be two attempted inserts for a single parent resource. Adding upsert? true to the parent resource's create action fixes the issue but I guess I was wondering if I have the resources set up wrong, such that Ash attempts to do two inserts for the same changesetHm...that sound strange
Are you sure you don't have duplicate parents?
I'm not sure what duplicate parents would look like? At the time of the test I am running, there are no parent resources in the db at all. The logs look like this if I call bulk_create from a shell

Yeah, maybe I'm not making sense 😂 I've picked something up from ElixirConf and I'm not fully um...smart right now 😂
Since you said you had a reproduction, please open an issue and lay out what you're doing and include a link to the reproduction. Maybe someone else can help though if they can spot the issue
Sounds good, thanks
Feel better!