Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf9bd171

Browse files
authored
fix: handle results that can't be mapped to the changeset in bulk_create (#638)
* fix: handle results that can't be mapped to the changeset in bulk_createIf the identity used has attibutes that can be generated by thedatalayer, we can't map the result back to the changeset and we need tojust zip the results with the changesets and return them that way.* refactor: do a simple check for `upsert?` instead
1 parentd54f93c commitf9bd171

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

‎lib/data_layer.ex‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,34 +2124,57 @@ defmodule AshPostgres.DataLayer do
21242124
end)
21252125

21262126
results=
2127-
changesets
2128-
|>Enum.map(fnchangeset->
2129-
identity=
2130-
changeset.attributes
2131-
|>Map.take(keys)
2127+
ifopts[:upsert?]do
2128+
changesets
2129+
|>Enum.map(fnchangeset->
2130+
identity=
2131+
changeset.attributes
2132+
|>Map.take(keys)
21322133

2133-
result_for_changeset=Map.get(results_by_identity,identity)
2134+
result_for_changeset=Map.get(results_by_identity,identity)
21342135

2135-
ifresult_for_changesetdo
2136+
ifresult_for_changesetdo
2137+
if!opts[:upsert?]do
2138+
maybe_create_tenant!(resource,result_for_changeset)
2139+
end
2140+
2141+
caseget_bulk_operation_metadata(changeset)do
2142+
{index,metadata_key}->
2143+
Ash.Resource.put_metadata(result_for_changeset,metadata_key,index)
2144+
2145+
nil->
2146+
# Compatibility fallback
2147+
Ash.Resource.put_metadata(
2148+
result_for_changeset,
2149+
:bulk_create_index,
2150+
changeset.context[:bulk_create][:index]
2151+
)
2152+
end
2153+
end
2154+
end)
2155+
|>Enum.filter(&&1)
2156+
else
2157+
results
2158+
|>Enum.zip(changesets)
2159+
|>Enum.map(fn{result,changeset}->
21362160
if!opts[:upsert?]do
2137-
maybe_create_tenant!(resource,result_for_changeset)
2161+
maybe_create_tenant!(resource,result)
21382162
end
21392163

21402164
caseget_bulk_operation_metadata(changeset)do
21412165
{index,metadata_key}->
2142-
Ash.Resource.put_metadata(result_for_changeset,metadata_key,index)
2166+
Ash.Resource.put_metadata(result,metadata_key,index)
21432167

21442168
nil->
21452169
# Compatibility fallback
21462170
Ash.Resource.put_metadata(
2147-
result_for_changeset,
2171+
result,
21482172
:bulk_create_index,
21492173
changeset.context[:bulk_create][:index]
21502174
)
21512175
end
2152-
end
2153-
end)
2154-
|>Enum.filter(&&1)
2176+
end)
2177+
end
21552178

21562179
{:ok,results}
21572180
end

‎test/bulk_create_test.exs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
defmoduleAshPostgres.BulkCreateTestdo
66
useAshPostgres.RepoCase,async:false
7-
aliasAshPostgres.Test.{Post,Record}
7+
aliasAshPostgres.Test.{IntegerPost,Post,Record}
88

99
requireAsh.Query
1010
importAsh.Expr
@@ -356,6 +356,18 @@ defmodule AshPostgres.BulkCreateTest do
356356
|>Ash.Query.load(:ratings)
357357
|>Ash.read!()
358358
end
359+
360+
test"bulk creates with integer primary key return records"do
361+
%Ash.BulkResult{records:records}=
362+
Ash.bulk_create!(
363+
[%{title:"first"},%{title:"second"},%{title:"third"}],
364+
IntegerPost,
365+
:create,
366+
return_records?:true
367+
)
368+
369+
assertlength(records)==3
370+
end
359371
end
360372

361373
describe"validation errors"do

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp