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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement