Ash FrameworkAF
Ash Framework8mo ago
12 replies
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


Parent resource:
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)


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
Contribute to averypeck/helpdesk development by creating an account on GitHub.
helpdesk/test/helpdesk_test.exs at 81eb8e9ab6e12454d3294cc925c5f51b...
Was this page helpful?