mrjbj
How to insert parent and child via Ecto.Multi?
I have a model containing Transactions and Splits. The splits are children of transactions. I’m trying to first insert a new transaction, pick-up the freshly generated transaction.uuid, and use that value to insert into the child row to be created in the next step. I’m having trouble finding a way to reference the fresh minted uuid from the first step in the insert on child. What would be considered a good way to handle this? Thanks.
test "SPLIT inserts via Ecto.Multi style transaction." do
multi =
Ecto.Multi.new()
|> Ecto.Multi.insert(:transaction, create_transaction(transaction_map()))
|> Ecto.Multi.insert(:split, create_split(split_map(:transaction)))
case Repo.transaction(multi) do
{:ok, _results} ->
IO.puts("Success")
{:error, :transaction, changeset, _changes} ->
IO.inspect(changeset.errors, label: "Transaction insert failed.")
{:error, :split, changeset, _changes} ->
IO.inspect(changeset.errors, label: "split insert failed.")
end
end
Marked As Solved
tomkonidas
You would be able to get it like this using Ecto.Multi.insert/4
test "SPLIT inserts via Ecto.Multi style transaction." do
multi =
Ecto.Multi.new()
|> Ecto.Multi.insert(:transaction, create_transaction(transaction_map()))
|> Ecto.Multi.insert(:split, fn %{transaction: transaction} ->
# `transaction` is the inserted transaction a step before
create_split(transaction.id)
end)
case Repo.transaction(multi) do
{:ok, _results} ->
IO.puts("Success")
{:error, :transaction, changeset, _changes} ->
IO.inspect(changeset.errors, label: "Transaction insert failed.")
{:error, :split, changeset, _changes} ->
IO.inspect(changeset.errors, label: "split insert failed.")
end
end
3
Popular in Questions
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
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
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Other popular topics
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
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...
New
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









