Ecto.Multi insert_all - how to get new ids?

I have a nice Ecto.Multi operation working nicely:

    insert_jobs =
      Ecto.Multi.new()
      |> Ecto.Multi.insert_all(:jobs, Job, formatted_jobs)
      |> Repo.transaction()

    case insert_jobs do
      {:ok, jobs} ->
        program = Repo.get(Program, program_id)

        IEx.pry()

How can I get the newly created row ids from my operation?

The output of my pry:

pry(1)> jobs
%{jobs: {1, nil}}

:wave:

There is a :returning option (Multi.insert_all accepts the same options as Repo.insert_all), maybe it could be used?

|> Ecto.Multi.insert_all(:jobs, Job, formatted_jobs, returning: [:id])

The jobs would look something like

pry(1)> jobs
%{jobs: {1, [%{id: some_id}]}}
10 Likes

Awesome thanks