jan-swiatek
Ecto.Multi error handling
Hi
,
I want to insert user and company at once (user belongs to company, company can have many users) using Ecto.Multi. Here is my current form:
<.simple_form :let={f} for={@changeset} action={~p"/users/register"}>
<.inputs_for :let={company} field={f[:company]}>
<.input field={company[:name]} type="text" label="Company" />
</.inputs_for>
<.input field={f[:email]} type="email" label="Email" required />
<.input field={f[:password]} type="password" label="Password" required />
<:actions>
<.button phx-disable-with="Creating account..." class="w-full">Create an account</.button>
</:actions>
</.simple_form>
and Ecto.Multi transaction:
def register(user_params, company_params) do
Multi.new()
|> Multi.run(:company, fn _repo, _changes ->
case Companies.create_company(company_params) do
{:ok, %{company: company}} -> {:ok, company}
{:error, :company, changeset, _} -> {:error, changeset}
end
end)
|> Multi.run(:user, fn _repo, %{company: company} ->
user_params
|> Map.put("company_id", company.id)
|> Accounts.register_user()
end)
|> Repo.transaction()
end
However if something went wrong while creating company (e.g. name was already taken) I got Company changeset, what causes an error: “could not generate inputs for :company from MyApp.Companies” (it expects User changeset). What is the most idiomatic approach in this case? I can’t use cast_assoc, since create_company is Multi transaction itself (it also invokes some additional steps).
Most Liked
dimitarvp
jan-swiatek
Thanks for replies. I ended up with reorganising code a bit (I changed order of functions in transactions) and I was able to use cast_assoc ![]()
1
Popular in Questions
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
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
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
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
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
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
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
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
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
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









