polymorphic_embed icon indicating copy to clipboard operation
polymorphic_embed copied to clipboard

[Advice] Using this library with insert_all

Open coladarci opened this issue 2 years ago • 1 comments

Currently, from what I can tell, this library requires a changeset for all inserts.

We have a use case where we need to create 10s of thousands of records using insert_all that makes use of a polymorphic_embeds field.

We are using insert_all for performance reasons as looping through and making 10k inserts is an issue.

This results in the following error

        | ** (RuntimeError) polymorphic_embed is not able to add an autogenerated key without casting through cast_polymorphic_embed/3
        |     (polymorphic_embed 2.0.1) lib/polymorphic_embed.ex:282: PolymorphicEmbed.dump/3
        |     (ecto 3.9.2) lib/ecto/type.ex:941: Ecto.Type.process_dumpers/3
        |     (ecto 3.9.2) lib/ecto/repo/schema.ex:1006: Ecto.Repo.Schema.dump_field!/6

When using embeds_many, the same problem can exist BUT if you pass in a struct with all the necessary fields, the insert succeeds - this is discussed here: https://elixirforum.com/t/using-repo-insert-all-when-your-schema-has-embeds-many/14219

Is there a way to force a known json blob into the field when using insert_all?

Many thanks!

coladarci avatar May 23 '23 22:05 coladarci

+1

pedro-laviano-peek avatar May 24 '23 13:05 pedro-laviano-peek