I have multiple concurrent tasks, all of them trying to check record existance, then, if not exist, will insert one.
Unfortunately, I end up with duplicate writing to the record into DB, as it seems that all of tasks decides that the record does not exist at the same time, then all of them do insertion.
The desired behavior, is that I got the insertion only once, and then, other tasks would recognize the existence of just inserted record.
Here is my attempt:
alias MyApp.Parent, as: Parent
alias MyApp.Repo, as: Repo
changeset = Parent.changeset(%Parent{}, model)
case Repo.all(from p in Parent, where: p.mobile_number == ^model.mobile_number) do
[] ->
#does not exist
case Repo.insert_or_update(changeset) do
{:ok, %MyApp.Parent{ id: parent_id }} -> parent_id
error_message -> nil
end
[parent_get_by|t] ->
#already exist
%MyApp.Parent{ id: parent_id }= parent_get_by
parent_id
end
Any help is appreciated!