erlang - Why insert_all does not work with changesets? - Stack Overflow

admin2025-04-22  1

Does someone know, what is the limitation in Ecto that the insert all is not made to be working with changesets? Usually I need to create change sets for all entries and then map them to normal map(at the same time removing the virtual fields if existing and the not loaded entities).

Does someone know, what is the limitation in Ecto that the insert all is not made to be working with changesets? Usually I need to create change sets for all entries and then map them to normal map(at the same time removing the virtual fields if existing and the not loaded entities).

Share Improve this question asked Jan 21 at 15:08 TanoTano 1,3771 gold badge19 silver badges40 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Repo.insert_all can not insert into multiple tables. Since changesets can do, e.g. cast_assoc to create/update/delete multiple associations, this conflicts with the way bulk inserts work.

Can you describe your data model, so it will be easier to talk about possible approaches to achieve what you want?

The point of Changeset is to track changes to a particular structure. Imagine if you have [change(user1, name: "Bob"), change(user2, address: "Bob Street 1")]; What would you expect the resulting bulk insert operation to look like? The only useful thing would be basically for cs <- changesets, do: Repo.insert(cs) which you could just do yourself. The application for insert_all and update_all is to do bulk insert/update of uniformly shaped data and/or from queries, which changesets are not a good fit for.

I believe you're wanting https://hexdocs.pm/ecto/Ecto.Multi.html and haven't bumped into it yet in the docs.

转载请注明原文地址:http://anycun.com/QandA/1745301631a90585.html