Ecto embeds_one_of a few schemas

Hello! I am trying to create a few different embedded schemas for storing JSON in a jsonb column in postgres using ecto. The problem is that as I have a few different schemas I am not sure how to add them to the jsonb column as there is only an embeds_one and an embeds_many option and you can only specify one schema. Is there a way to specify that this column can embed one of a few options of schemas (like pass in a list of schemas)? Or maybe this is a feature request for ecto. Or maybe this is a faulty design on my part… Thank you for reading and for any insight!

You can imitate enums / sum types by having one root schema that only has type and data fields. The type field will determine the shape of the data field. I’ve done it before, it’s not the prettiest code but it does work.

Take a look at polymorphic_embed - it does pretty much exactly what you’re describing.

3 Likes

Works very nicely! Thanks