AF
Ash Framework•5mo ago
avery

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:
actions do
create :create do
accept [:*]
argument :project, :map, allow_nil?: false
change manage_relationship(:project, type: :create)
end
end

relationships do
belongs_to :project, Project, allow_nil?: false, attribute_writable?: true
end
actions do
create :create do
accept [:*]
argument :project, :map, allow_nil?: false
change manage_relationship(:project, type: :create)
end
end

relationships do
belongs_to :project, Project, allow_nil?: false, attribute_writable?: true
end
Parent resource:
actions do
create :create do
primary? true
accept [:*]
end
end

relationships do
has_many :project_notes, ProjectNote
actions do
create :create do
primary? true
accept [:*]
end
end

relationships do
has_many :project_notes, ProjectNote
Bulk create:
create_input = %{
text: "This is a test project note",
project: %{
name: "Test Project"
}
}

Ash.bulk_create!([create_input], Helpdesk.Projects.ProjectNote, :create)
create_input = %{
text: "This is a test project note",
project: %{
name: "Test Project"
}
}

Ash.bulk_create!([create_input], Helpdesk.Projects.ProjectNote, :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-L55
GitHub
helpdesk/test/helpdesk_test.exs at 81eb8e9ab6e12454d3294cc925c5f51b...
Contribute to averypeck/helpdesk development by creating an account on GitHub.
6 Replies
ZachDaniel
ZachDaniel•5mo ago
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.
avery
averyOP•5mo ago
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 changeset
ZachDaniel
ZachDaniel•5mo ago
Hm...that sound strange Are you sure you don't have duplicate parents?
avery
averyOP•5mo ago
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
No description
ZachDaniel
ZachDaniel•5mo ago
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
avery
averyOP•5mo ago
Sounds good, thanks Feel better!

Did you find this page helpful?