1stSolo

1stSolo

Optimistic lock solution for "embedded"/associated object. Entity mapped to Union

Hello.

I have a Parent Absinthe & Ecto schemas and there’s an embedded child object associated to it (1:1, so the fully inflated object is basically a union of the underlying tables, but it’s not implemented literally that way become some parents may have several fields of Child type, e.g. left_child, 'right_child). I didn't want the Absinthe client to manipulate children directly, so we have a graphQL mutation such as updateParentthat takesparent_idanddataarguments, and withindatayou can provide fields ofParentobject as well as fields ofleft_childorright_child`, e.g.

mutation {
updateParent(id: , data: { name: “new name”, left_child: { diagram: “{ some: json }”, right_child: nil, lock_version: <current_parent_lock_version>}}) {
id, name, left_child {id, diagram}, right_child {id, diagram}
}

We have standard optimistic lock on parent and child:

  def changeset(parent, attrs \\ %{}) do
    parent
    |> cast(attrs, @cast_attrs)
    |> validate_required(@rqrd_attrs)
    |> update_children(attrs)
    |> optimistic_lock(:lock_version)
  end

Here’s the usecase I am having a problem with.

If my mutation arguments don’t contain any updates for the parent object the changset is empty and parent.lock_version won’t get incremented. Also, in order to perform updates on fields of Child type, they need to be preloaded:

parent = Repo.preload(parent, [:left_child, :right_child])

which loads their current lock_version values, so there would almost never be a failed optimistic lock because any update of child would have the most current lock_version.

How can I share the optimistic lock of parent with the children, so that if a child is updated the lock_version on the parent is incremented even if parent is not updated? I don’t want to expose children’s lock_version to the client - I want all updates to go through parent.

Semantically, it’s as if children were just fields on the parent, in which case it would work like normal optimistic lock on the parent, but it’s not desirable because children have lots of fields and I don’t want them on the parent table as columns. But this is basically an entity mapped into a union of tables, sometimes parent union-ed with dependent table more than once (as in case of left_child and right_child). I hope that makes sense…

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement