Not able to upsert when given "updated_at" field in params

I’m trying to upsert a record with the updated_at field in the params like below.

Ash.create!(
  MyApp.Resource, 
  %{
    ...
    updated_at: "2022-10-28T08:23:58.169Z"
  },
  upsert?: true
)

Please note that I’ve set writable? to true for the timestamp field updated_at. When I run the above the function, I’m getting the following error:

** (Ash.Error.Unknown) Unknown Error

* ** (Ecto.QueryError) duplicate field `updated_at` for `update_all` in query:

from c0 in MyApp.Resource,
  as: 0,
  update: [
  set: [
    ...
    updated_at:
      fragment("COALESCE(EXCLUDED.?, ?)", literal("updated_at"), ^~U[2024-10-01 06:42:56.713776Z]),
    updated_at:
      fragment("COALESCE(EXCLUDED.?, ?)", literal("updated_at"), ^~U[2024-10-01 06:42:56.713776Z])
  ]
]

  (elixir 1.17.2) lib/enum.ex:2531: Enum."-reduce/3-lists^foldl/2-0-"/3
  (ecto 3.12.3) lib/ecto/repo/schema.ex:825: Ecto.Repo.Schema.on_conflict_query/6
  (ecto 3.12.3) lib/ecto/repo/schema.ex:55: Ecto.Repo.Schema.do_insert_all/7
  (ash_postgres 2.4.1) lib/data_layer.ex:1809: AshPostgres.DataLayer.bulk_create/3
  (ash_postgres 2.4.1) lib/data_layer.ex:2560: AshPostgres.DataLayer.upsert/4
  (ash 3.4.16) lib/ash/actions/create/create.ex:357: anonymous fn/6 in Ash.Actions.Create.commit/3
  (ash 3.4.16) lib/ash/changeset/changeset.ex:3703: Ash.Changeset.run_around_actions/2
  (ash 3.4.16) lib/ash/changeset/changeset.ex:3238: anonymous fn/3 in Ash.Changeset.with_hooks/3

It seems like it’s trying to set updated_at twice. The same is not happening when I just try to insert instead of upsert. I’m not very sure why this is happening. Any ideas how I can fix this?

Hey there! Can you try out main of ash_postgres? It should resolve this issue.

1 Like