fireproofsocks

fireproofsocks

Ecto.multi with insert and insert_all?

Is it possible to use an Ecto.Multi to do an insert and an insert_all where the insert_all needs the primary key resulting from the first insert? E.g. creating a parent record with numerous child records in one transaction.

The insert option can use a 3rd argument of changeset_or_struct_or_fun, but the insert_all function doesn’t allow for a function… so there doesn’t appear to be a good way to capture the primary key that gets generated.

I can wrap the steps inside of a Repo.transaction callback function like this:

Repo.transaction fn ->
  with {:ok, parent} <- ParentContext.create(params),
       {:ok, _children} <- create_children(parent, params)
    do

  else
    {:error, e} -> Repo.rollback(e)
  end
end

but I was curious if the multi supported it. Thanks!

Marked As Solved

axelson

axelson

Scenic Core Team

Yeah you can using Multi.merge/2 (or Multi.run). It looks something like this:

Multi.new()
|> Multi.insert(:user, User.changeset(%User{}, params))
|> Multi.merge(fn %{user: user} ->
  # That is the inserted user from the first part of the multi
  Multi.new()
  |> Multi.insert_all(:posts, build_insert_all_posts_changeset(user))
end)
|> Repo.transaction()
12
Post #2

Also Liked

alxvallejo

alxvallejo

What is this function??

LostKobrakai

LostKobrakai

You can’t directly. Repo.*_all are lower level APIs, which don’t support many schema or changeset features. You can map things from changeset to values for insert_all manually if you don’t need any of the not really supported parts.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement