sabri

sabri

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?

Showing Posts 1 to 8

outlog

outlog

can’t quite make out your code.. but look at insert_all which does an “upsert” Ecto.Repo — Ecto v3.14.0 then you decide how to handle the conflict.

sabri

sabri OP

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..

OvermindDL1

OvermindDL1

When you have such combined things you really need to be using Ecto.Multi.

sabri

sabri OP

Can I use Ecto.Multi to add records and associate them too? as I have belongs to relation

OvermindDL1

OvermindDL1

Yep. Ecto.Multi is just ‘transactions’ in Ecto, so definitely yes. :slight_smile:

outlog

outlog

sorry, was to fast and there was a bug in the documentation, I guess I meant use upsert options for insert (doc bug fixed here Repo: Reference correct function in insert/2 upsert docs (#1909) · elixir-ecto/ecto@acf8016 · GitHub)

{:ok, %Trackware.Schools.Parent{ id: father_id }} = SRepo.insert(father_head , on_conflict: :replace_all, conflict_target: :mobile_number)

should be able to replace the entire code snippet you posted.

I would guess this would still be needed in a multi, as you might have student siblings that gives you these conflicts.

sabri

sabri OP

Is there an example for such case in Ecto.multi? Inserting two records in different tables and set the foreign key of one to point to the other?

OvermindDL1

OvermindDL1

Well you can insert the other first then insert the original that points to the first, that is the usual way (in a transaction of course).

Or you can insert the first with a null foreign key, then the other, then update the first with with the key, but that is too many calls.

Basically for the first method just Multi.insert the first, then Multi.run to grab the id and insert the second. :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews