Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2142,8 +2142,12 @@ defmodule AshPostgres.DataLayer do
end

# Compatibility fallback
Ash.Resource.put_metadata(
result_for_changeset,
result_for_changeset
|> Ash.Resource.put_metadata(
:bulk_create_index,
changeset.context[:bulk_create][:index]
)
|> Ash.Resource.put_metadata(
:bulk_action_ref,
changeset.context[:bulk_create][:ref]
)
Expand All @@ -2159,8 +2163,12 @@ defmodule AshPostgres.DataLayer do
end

# Compatibility fallback
Ash.Resource.put_metadata(
result,
result
|> Ash.Resource.put_metadata(
:bulk_create_index,
changeset.context[:bulk_create][:index]
)
|> Ash.Resource.put_metadata(
:bulk_action_ref,
changeset.context[:bulk_create][:ref]
)
Expand Down
17 changes: 17 additions & 0 deletions test/bulk_create_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,21 @@ defmodule AshPostgres.BulkCreateTest do
end)
end
end

describe "bulk_create with sorted?: true" do
test "works without error" do
# This test reproduces https://github.com/ash-project/ash/issues/2452
# generate_many uses sorted?: true which requires bulk_create_index metadata
results =
Ash.bulk_create!(
[%{title: "post_1"}, %{title: "post_2"}, %{title: "post_3"}],
Post,
:create,
return_records?: true,
sorted?: true
)

assert length(results.records) == 3
end
end
end
Loading