I have following code:
MyModel.changeset(data)
|> Repo.insert()
I would like to insert 3 times the same row to MyModel in a single transaction. Does Ecto have something like:
repeat_times(3, MyModel.changeset(data))
|> Repo.insert()
?
Have you seen https://hexdocs.pm/ecto/Ecto.Multi.html? It actually operates multiple functions and performs them in a single db transaction.
The short answer is no, Ecto 3.x doesn't give you something like that out of the box...
If you're looking for an insert_all type operation, have you checked out Repo.insert_all? That's the closest function I can think of that might give you what you're looking for. You'd have to write your own custom function or something to manually construct the list of repeat "MyModel" records. And if you wanted to incorporate changesets, you'd again have to make your own function.
Ecto.Repo.transaction/2. The latter is impossible out of the box. - Aleksei Matiushkin