flp

flp

Multi.insert_all/Repo.insert_all fail with Enumerable not implemented

I tried to use the insert_all functions to batch insert 2 records, but it always fails with Enumerable not implemented for Model:

Repo.insert_all(Model, [%Model{id: 1, friend_id: 4}, %Model{id: 4, friend_id: 1}])

I got it working by inserting every record at once:

Multi.new
|> Multi.insert(:model1, %Model{id: 1, friend_id: 4})
|> Multi.insert(:model2, %Model{id: 4, friend_id: 1})
|> Repo.transaction

Maybe my misunderstanding is that only normal maps are supported?

Repo.insert_all(Model, [%{id: 1, friend_id: 4}, %{id: 4, friend_id: 1}])

Unfortunately, then the timestamp values are not automatically set:
null value in column "inserted_at" violates not-null constraint

Is there any way to achieve the batch insertion with phoenix models and what exactly is my mistake or misunderstanding?

Most Liked

kylethebaker

kylethebaker

It looks like only lists of maps or keyword lists are supported. From the @spec:

entries :: [map | Keyword.t]

There is some explanation in the documentation about why this is the case:

However any other autogenerated value, like timestamps, won’t be autogenerated when using insert_all/3. This is by design as this function aims to be a more direct way to insert data into the database without the conveniences of insert/2. This is also consistent with update_all/3 that does not handle timestamps as well.

It is also not possible to use insert_all to insert across multiple tables, therefore associations are not supported.

It does feel like a shortcoming though, I’m not sure why it doesn’t work with schemas/structs.

EDIT: You could create a utility around Multi like this:

def insert_multiple(entries) do
  multi =
    Enum.reduce entries, Multi.new(), fn(multi, entry) ->
      Multi.insert(multi, entry.id, entry)
    end
  Repo.transaction(multi)
end

It depends on the struct to have an id field for the multi name, but you could change that to anything I think, it just needs to be unique.

idi527

idi527

You can insert your data with timestamps like so

to_insert =
  [%{id: 1, friend_id: 4}, %{id: 4, friend_id: 1}]
  |> Enum.map(&Map.put(&1, :inserted_at, NaiveDateTime.utc_now())) 

Repo.insert_all(Model, to_insert)

Where Next?

Popular in Questions Top

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement