Skip to content

Commit 99e8029

Browse files
committed
improvement: fix upsert_fields behavior for upserts
1 parent b00dcf2 commit 99e8029

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

config/config.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import Config
22

3-
config :ash, :use_all_identities_in_manage_relationship?, false
4-
53
if Mix.env() == :dev do
64
config :git_ops,
75
mix_project: AshPostgres.MixProject,

lib/data_layer.ex

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,15 @@ defmodule AshPostgres.DataLayer do
11511151

11521152
opts =
11531153
if options[:upsert?] do
1154+
on_conflict =
1155+
if options[:upsert_set] do
1156+
[set: options[:upsert_set]]
1157+
else
1158+
{:replace, options[:upsert_fields] || []}
1159+
end
1160+
11541161
opts
1155-
|> Keyword.put(:on_conflict, {:replace, options[:upsert_fields] || []})
1162+
|> Keyword.put(:on_conflict, on_conflict)
11561163
|> Keyword.put(
11571164
:conflict_target,
11581165
conflict_target(
@@ -1761,12 +1768,22 @@ defmodule AshPostgres.DataLayer do
17611768
|> update_defaults()
17621769
|> Keyword.merge(explicitly_changing_attributes)
17631770

1771+
on_conflict =
1772+
if changeset.context[:private][:upsert_fields] do
1773+
Keyword.take(
1774+
on_conflict,
1775+
changeset.context[:private][:upsert_fields]
1776+
)
1777+
else
1778+
on_conflict
1779+
end
1780+
17641781
case bulk_create(resource, [changeset], %{
17651782
single?: true,
17661783
upsert?: true,
17671784
tenant: changeset.tenant,
17681785
upsert_keys: keys,
1769-
upsert_fields: Keyword.keys(on_conflict),
1786+
upsert_set: on_conflict,
17701787
return_records?: true
17711788
}) do
17721789
{:ok, [result]} ->

0 commit comments

Comments
 (0)