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

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
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
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
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 Postgres...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement