How to chain Ecto.Multi.insert_all calls

Hello all,

I have an Ecto.Multi chain that creates a list of chat groups with Multi.insert_all, and then creates chats_users join table records for all of those chat groups.

Unfortunately, it appears the first insert_all call only returns a tuple with the number of records created. e.g. {16, nil}

Is there a way to get a list of the all the records to iterate through them?
|> Multi.insert_all(:chats, Chat,
      # chat for every team, and every team + org members
      chats = Enum.reduce(cohort.teams, [], fn team, chat_list ->
        chat_1 = %{
          name: "#{}", 
          type: "Team",
          inserted_at: now,
          updated_at: now}
        chat_2 = %{
          name: "#{} + #{}", 
          type: "Cohort Team", 
          parent_organization_id: cohort.organization.parent_organization_id,
          organization_id: cohort.organization_id,
          inserted_at: now,
          updated_at: now}
        |> List.insert_at(-1, chat_1)
        |> List.insert_at(-1, chat_2)
    |> Multi.insert_all(:chats_users, ChatUser,
      fn %{chats: chats} ->
      ## The above has a tuple {16, nil} and not the list of records

You can see the final line fn %{chats: chats} -> ends up with {16, nil} and not with a list of the chats.

Appreciate any help, thank you!

1 Like

Maybe this would help Ecto.Multi insert_all - how to get new ids?