Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch insert with Ecto

Tags:

elixir

ecto

I'm trying to insert 800 changesets using a multirow INSERT query with Ecto. I'm using postgres. I see that postgrex supports this now. Is it possible yet with Ecto?

I'm also open to doing this via prepared statement if that's possible.

like image 282
Josh Rieken Avatar asked May 05 '16 03:05

Josh Rieken


People also ask

What is ecto changeset?

8.4) Changesets allow filtering, casting, validation and definition of constraints when manipulating structs. There is an example of working with changesets in the introductory documentation in the Ecto module. The functions cast/4 and change/2 are the usual entry points for creating changesets.

What is an ecto schema?

An Ecto schema maps external data into Elixir structs. The definition of the schema is possible through two main APIs: schema/2 and embedded_schema/1 . schema/2 is typically used to map data from a persisted source, usually a database table, into Elixir structs and vice-versa.

Is ecto ORM?

Unlike ActiveRecord, Ecto is not an ORM, but a library that enables the use of Elixir to write queries and interact with the database. Ecto is a domain specific language for writing queries and interacting with databases in Elixir.


1 Answers

Ecto 2.0 will support Repo.insert_all/3 (current latest version is 2.0.0-rc.4 which supports this), but it doesn't support inserting changesets, only raw Maps or Keyword lists, so you'll have to filter all valid changesets and extract the fields from them yourself like you would have needed to if you used the Postgrex bulk insert feature.

like image 123
Dogbert Avatar answered Sep 20 '22 12:09

Dogbert