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.

Last Post!

ca1989

ca1989

Thanks for the clarification.

So, is there any example on how to insert a bunch of “children” in a Multi with validation?

Will open a new thread though. Don’t want to necrobump.

Where Next?

Popular in Questions Top

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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New

We're in Beta

About us Mission Statement