BryanJBryce

BryanJBryce

I want to create an upsert action that will create a new record if no id is contained in the request and update the record if there is an id in the request. I thought that this might be the default behavior without upsert_identity or upset_fields options being set. However, none of these configurations works:

Action definition

    create :save do
      upsert? true
      upsert_identity :unique_id
      upsert_fields {:replace_all_except, [:id]}
    end

Code Interface definition
define :save, action: :save

  identities do
    identity :unique_id, :id
  end

However when I call:

instance = Resource.save!(%{name: "Name"}, tenant: "tenant")
instance2 = Resource.save!(%{name: "Name2", id: instance.id}, tenant: "tenant")

a second record is created in the database.

Showing Posts 7 to 1

BryanJBryce

BryanJBryce OP

That did it. Thanks for the help!

zachdaniel

zachdaniel

Creator of Ash

Ah, okay right. So the issue is that the id isn’t writable by your save action. You can do this:

uuid_primary_key :id, writable?: true

to make it writable.

BryanJBryce

BryanJBryce OP

FYI, this output is going back to just setting upsert?: true and removing the other configuration

BryanJBryce

BryanJBryce OP

Hrm, the id from the first isn’t showing up in the second…

BryanJBryce

BryanJBryce OP

iex(1)> inst = Register.save!(%{location: "Back Counter", dejavoo_register_id: "22", dejavoo_auth_key: "puts", printer_id: "yex", client_group_id: "1065b90b-618f-404a-bd16-6cad6e949d82"}, tenant: "dreambean")
[debug] QUERY OK db=1.5ms idle=1302.3ms
begin []
↳ anonymous fn/3 in Ash.Changeset.with_hooks/3, at: lib/ash/changeset/changeset.ex:2488
[debug] QUERY OK source="registers" db=5.1ms
INSERT INTO "dreambean"."registers" AS r0 ("id","location","dejavoo_register_id","dejavoo_auth_key","printer_id","is_locked","created_at","updated_at","client_group_id") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) ON CONFLICT ("id") DO UPDATE SET "location" = EXCLUDED."location", "dejavoo_register_id" = EXCLUDED."dejavoo_register_id", "dejavoo_auth_key" = EXCLUDED."dejavoo_auth_key", "printer_id" = EXCLUDED."printer_id", "client_group_id" = EXCLUDED."client_group_id", "updated_at" = COALESCE(EXCLUDED."updated_at", $10) RETURNING "client_group_id","updated_at","created_at","is_locked","printer_id","dejavoo_auth_key","dejavoo_register_id","location","id" ["1b3cf4b1-9459-4fef-a392-539f1d634670", "Back Counter", "22", "puts", "yex", true, ~U[2024-03-19 19:27:32.324905Z], ~U[2024-03-19 19:27:32.324905Z], "1065b90b-618f-404a-bd16-6cad6e949d82", ~U[2024-03-19 19:27:32.336228Z]]
↳ AshPostgres.DataLayer.bulk_create/3, at: lib/data_layer.ex:1412
[debug] QUERY OK db=0.5ms
commit []
↳ anonymous fn/3 in Ash.Changeset.with_hooks/3, at: lib/ash/changeset/changeset.ex:2488
#PointOfSale.Stores.Register<
  tills: #Ash.NotLoaded<:relationship, field: :tills>,
  active_order: #Ash.NotLoaded<:relationship, field: :active_order>,
  client_group: #Ash.NotLoaded<:relationship, field: :client_group>,
  __meta__: #Ecto.Schema.Metadata<:loaded, "dreambean", "registers">,
  id: "1b3cf4b1-9459-4fef-a392-539f1d634670",
  location: "Back Counter",
  dejavoo_register_id: "22",
  dejavoo_auth_key: "puts",
  printer_id: "yex",
  is_locked: true,
  created_at: ~U[2024-03-19 19:27:32.324905Z],
  updated_at: ~U[2024-03-19 19:27:32.324905Z],
  client_group_id: "1065b90b-618f-404a-bd16-6cad6e949d82",
  aggregates: %{},
  calculations: %{},
  ...
>
iex(2)> Register.save!(%{location: "Back Counter", dejavoo_register_id: "88", dejavoo_auth_key: "puts", printer_id: "yex", client_group_id: "1065b90b-618f-404a-bd16-6cad6e949d82", id: inst.id}, tenant: "dreambean")
[debug] QUERY OK db=0.8ms idle=1266.9ms
begin []
↳ anonymous fn/3 in Ash.Changeset.with_hooks/3, at: lib/ash/changeset/changeset.ex:2488
[debug] QUERY OK source="registers" db=5.2ms
INSERT INTO "dreambean"."registers" AS r0 ("id","location","dejavoo_register_id","dejavoo_auth_key","printer_id","is_locked","created_at","updated_at","client_group_id") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) ON CONFLICT ("id") DO UPDATE SET "location" = EXCLUDED."location", "dejavoo_register_id" = EXCLUDED."dejavoo_register_id", "dejavoo_auth_key" = EXCLUDED."dejavoo_auth_key", "printer_id" = EXCLUDED."printer_id", "client_group_id" = EXCLUDED."client_group_id", "updated_at" = COALESCE(EXCLUDED."updated_at", $10) RETURNING "client_group_id","updated_at","created_at","is_locked","printer_id","dejavoo_auth_key","dejavoo_register_id","location","id" ["a3bbbcce-93d6-4683-9db7-8ac440f5fc91", "Back Counter", "88", "puts", "yex", true, ~U[2024-03-19 19:27:46.293846Z], ~U[2024-03-19 19:27:46.293846Z], "1065b90b-618f-404a-bd16-6cad6e949d82", ~U[2024-03-19 19:27:46.295117Z]]
↳ AshPostgres.DataLayer.bulk_create/3, at: lib/data_layer.ex:1412
[debug] QUERY OK db=1.6ms
commit []
↳ anonymous fn/3 in Ash.Changeset.with_hooks/3, at: lib/ash/changeset/changeset.ex:2488
#PointOfSale.Stores.Register<
  tills: #Ash.NotLoaded<:relationship, field: :tills>,
  active_order: #Ash.NotLoaded<:relationship, field: :active_order>,
  client_group: #Ash.NotLoaded<:relationship, field: :client_group>,
  __meta__: #Ecto.Schema.Metadata<:loaded, "dreambean", "registers">,
  id: "a3bbbcce-93d6-4683-9db7-8ac440f5fc91",
  location: "Back Counter",
  dejavoo_register_id: "88",
  dejavoo_auth_key: "puts",
  printer_id: "yex",
  is_locked: true,
  created_at: ~U[2024-03-19 19:27:46.293846Z],
  updated_at: ~U[2024-03-19 19:27:46.293846Z],
  client_group_id: "1065b90b-618f-404a-bd16-6cad6e949d82",
  aggregates: %{},
  calculations: %{},
  ...
BryanJBryce

BryanJBryce OP

I was thinking that was the default, but when it didn’t work I tried adding the additional configuration. I’ll get that SQL for you.

zachdaniel

zachdaniel

Creator of Ash

Hmm…that does look like the right way to do it. Granted, you don’t need the unique_id constraint if id is the primary key. So it should have the behavior you said by default, meaning just upsert? true would be necessary. What SQL does the insert produce?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews