I am trying to insert many records into database, one by one as:
rec= SRepo.get_by(Trackware.Schools.Parent, mobile_number: father_head.mobile_number)
father_id= case rec do
nil ->
# its not inserted in DB before
{:ok, %Trackware.Schools.Parent{ id: father_id }} = SRepo.insert(father_head)
father_id
father ->
#its inserted before, return the record id
father.id
end
this work fine, however, if I try to use Stream.chunk and have the code above run in concurrent processes, I get error like:
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* unique: parents_mobile_number_uindex
If you would like to convert this constraint into an error, please
call unique_constraint/3 in your changeset and define the proper
constraint name. The changeset has not defined any constraint.
Although I check before insert a new record, but it seems such error is a result of concurrent processā¦
How can I solve this?
I tried to use insert_or_update but I got the same error as it seems processes are raising on the same line?
I did not use insert_all, because the student record belongs to one parent, and I need to save that parent, and get its id, then to set the parent_id in the student before saving itā¦