Search code examples
transactionselixirrepeatecto

Ecto: multiply changesets in one transaction


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()

?


Solution

  • 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.