Laetitia

Laetitia

Block child association to be created

Hi everybody,
I have 2 entities: parent and child.
The parent entity have a life cycle so it transits from one status to another a.k.a ‘open’ or ‘discard’.
When it transits (it is a transactionnal action with multi) any child can’t be created.
I check before create child that the parent is open but i need that check to be on the commit of the creation transaction because the parent can change while the child is beeing created. I don’t want the parent to be blocked because of children. The parent blocks the childs but the children don’t block the parent.
I thought on locking but it is not suited to my need because it solves concurrency and here the case is more a blocking in only one direction.
How can i solve this problem?
Do you think using prepare_change is suffisant and safe?
I hope i expressed clearly.
Thank you all

.

Marked As Solved

sasajuric

sasajuric

Author of Elixir In Action

This was my first thought. Specifying a for share lock while checking that the parent is open should guarantee consistent behaviour while allowing some concurrency (depending on whether parent is otherwise updated in the transaction). Explicit locking can be done in Ecto with lock.

Also Liked

evadne

evadne

If you are really serious about this, then you can implement some database level change to forbid changes to the child while the parent is locked (maybe an exclusive lock on the parent, advisory locks would work well too) but that can seriously gimp your performance

Another way to do it would be to funnel all mutations to the child through the same thing that mutates the parent (persistent process, works well with one host and less well as you add more) - but you can handle all this in the app

Triggers might or might not work as you can have the transaction that changes the parent and the transaction that changes the child start concurrently. Assuming that this is Postgres, the result might be surprising

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement