After_save returning :ok for individual file but not for multiple

Hey there,
In my code I don’t know that why I’m getting {:ok, files} message on return when uploading individual file. However on the same spot when i upload multiple then I am not receiving {:ok, files} message on return. Even after_save is working on multiple, like its uploading mutliple files but cannot recieve the :ok message.
Here’s my code:

  def create_files(files \\ [], after_save \\ &{:ok, &1}) do
    allFiles =
      for file <- files do
        |> File.changeset(file)
        |> Repo.insert()
        |> after_save(after_save)

    {:ok, files}

defp after_save({:ok, files}, func) do

    {:ok, _files} = func.(files)


  defp after_save(error, _func) do



We’ll after_save accepts a file, not files. It’s being called inside the for comprehension.

I’d suggest you don’t pipe the result of Repo insert into a function. Please read this for a nice example/explanation.

Do you actually want to silently ignore the errors? If you do, use insert_all so it happens in the one transaction.

It might be clearer to split the inserting and the subsequent acting on the results of that into different functions.

Thanks for your kind response.
Yep, you’re right. But when I use insert_all in my code then it says that “Protocol enumerable not implemeted for ectot.type for changesets…”.
I’m not having any issue from insert but the problem I’m facing from a long while is that after_save isn’t returning {:ok, files} even after removing the pipeline.

You’ve posted a similar statement in this thread and this thread.

My followup to all three is the same: what does create_files return in those cases? Is it crashing? Does anything appear in the logs?

Also worth considering: is that call to create_files passing an after_save? Could something in that code (not posted) be crashing and/or timing out?

Now the code I have attached above is the final code. Working perfectly fine.
The problem in this code is that
when I insert one file then it redirects to the main page and also shows the {:ok, files} message on that page.
But when i insert more than one it neither return to the main page nor shows the {:ok, files} message.

And when I remove the after_save function from my create_files function then it works so normal. Like either upload one or multiple it does return {:ok, files}.

Using after_save when i upload more than one then in log i recieve this error after inserting of files.

[debug] QUERY OK db=0.0ms idle=483.9ms
INSERT INTO `files` (`name`,`size`,`type`,`url`,`user_id`,`inserted_at`,`updated_at`) VALUES (?,?,?,?,?,?,?) ["2.PNG", 77831, "image/png", "/uploads/1d16a2f1-15cd-423e-b3c5-c28128f7f60a.png", 1, ~N[2021-10-21 13:31:55], ~N[2021-10-21 13:31:55]]      
[debug] QUERY OK db=0.0ms idle=499.9ms
INSERT INTO `files` (`name`,`size`,`type`,`url`,`user_id`,`inserted_at`,`updated_at`) VALUES (?,?,?,?,?,?,?) ["1.PNG", 212816, "image/png", "/uploads/a22489e0-b50a-4dea-bcff-6a2b26bfaa52.png", 1, ~N[2021-10-21 13:31:55], ~N[2021-10-21 13:31:55]]     
[error] GenServer #PID<0.2284.0> terminating
** (stop) exited in:<0.2291.0>, :consume_start, :infinity)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started```

I found a bug report with a similar error:

1 Like

When i upload multiple files without after_save then there’s no bug and redirecting to next page.
And even when i upload single file no bug.
This bug is only rising when i upload multiple files with after_save.