user20230119

user20230119

Upsert on Postgres without migrating an identiy?

How can I upsert without creating an identity on Postgres? I want to check if a record exists with a combination of parent_id and name for some records, but I do not want to enforce it with Postgres.

  postgres do
    table "entities"
    repo MyApp.Repo

    references do
      reference :children, index?: true, on_delete: :delete
      reference :parent, index?: true, on_delete: :delete
    end

    # skip_identities [:parent_id_child_name]
  end

  identities do
    identity :parent_id_child_name, [:parent_id, :name] do
      # eager_check? true
      # pre_check? true
    end
  end

  actions do
    defaults [:read]

    create :upsert_data do
      accept [:parent_id, :name, :data]
      upsert? true
      upsert_identity :parent_id_child_name
    end
  end

skip_identities cannot compile.

error: undefined function skip_identities/1 (there is no such import)`

eager_check? and pre_check? doesn’t update.

%Ash.Error.Changes.InvalidChanges{
       fields: [:parent_id, :name],
       message: "has already been taken",

Most Liked

zachdaniel

zachdaniel

Creator of Ash

It isn’t possible :slight_smile: Postgres requires a unique constraint to do an upsert. So you’d have to roll your own with application logic if you don’t want to have said unique constraint.

garrison

garrison

And you probably don’t want to do that either because IIRC the sort of “get or insert” semantics you would need to implement an upsert are literally impossible to achieve in Postgres outside of SERIALIZABLE. And even then I don’t believe it would be atomic (some transactions would just have to be retried).

(You can materialize the conflict onto another row/lock. I find this approach rather cursed, but I’ve had to do it in some cases - not upserts though, why fight the platform)

Is there any particular reason you are avoiding a constraint?

zachdaniel

zachdaniel

Creator of Ash

Correct, you can also use an advisory lock to avoid having to lock some specific row as well. We had roughly the same conversation a while back :laughing:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement