Is there no way to insert multiple entries in one go using Ecto.insert?

I listed the tradeoffs! There are situations where it’s possible to take the bad side of both tradeoffs.

There is probably some situation where this is a good idea but honestly I am struggling to think of a good example. There are cases where you need isolation for each insert but if you’re in that situation you wouldn’t be searching up a thread for insert_all() to begin with.

I’m concerned that someone new to this stuff might search this thread up, see the marked solution, and go with it without thinking when it’s almost certainly the wrong approach. I’m quite sure that you understand this (and you’re not the one who pinned the answer to the top of the thread!), but others may not.

As a side note, if you need isolation and you need to use insert_all() for performance there are some cases where you can use on_conflict to get the database to drop conflicting rows while inserting the rest in one batch. Handy, but doesn’t work for everything.

This was indeed ancient and yes nowadays I’d give another advice. My center assumption here was that validation per-record is still wanted.

Batch-inserting is actually a very thorny problem, and many questions arise in regards to it, my favorite one being “what happens if only one record fails to insert but many other records depend on it?”.

Of course at this point in time I’d make this much stricter. But as a quick reply of a question with barely any context of nearly 6 years ago, it was good enough. Maybe you dislike the blurb-like formats of some answers (like mine) but the implied context is that people should do their homework.

You might have noticed I love writing essays as responses, very much like yourself, but they are often unwelcome when it comes to “help I have problem X, what do I do?”. We can’t be 100% exhaustive in all answers.

2 Likes

You are correct on all points, but I think this ensuing discussion is at risk of obviating my original reply.

If you are someone who has just clicked on this thread from the internet and read the words “use Task.async_stream to parallelize individual inserts across cores”, erase that from your memory. Do not do that.

There are exceptions, and like you said they are not worth enumerating.