Search code examples
elixirphoenix-frameworkecto

How to retrieve id from previous inserted table key by Ecto Multi


I would like to retrieve id from previous inserted table primary key by Ecto Multi.

At first, I insert to A main table. then B details table needs A.id. I tried following code.

Multi.new()
  |> Multi.insert(:insert, main)
  |> Multi.insert_all(:insert_all, B, details)
  |> Repo.transaction()

However I have no idea how to retrieve A.id for insert table B. What I should do for it?


Solution

  • You can do something like the following example which creates a new User record and a new Email record (where the email record is associated to the parent user record via a user_id foreign key).

    alias Ecto.Multi
    
    user = get_user_params_from_form() # <-- or where-ever you are getting data
    email = get_email_params_from_form()
    
    Multi.new()
        |> Multi.insert(:user, User.changeset(%User{}, user))
        |> Multi.insert(
          :email,
          # Capture the id from the previous operation
          fn %{
               user: %User{
                 id: user_id
               }
             } ->
            Email.changeset(%Email{user_id: user_id}, email)
          end
        )
    

    I think this demonstrates the type of relationship you described. Hope it helps!