AndrewDryga

AndrewDryga

Hey guys. I know this is an old topic, but as I write more and more complex application with Elixir and Ecto I feel like we really need a way to let developers use dynamic embeds.

Few words about our use case, we have a Transport schema which stores various common fields and settings of a transport. But depending on transport type (eg. Twillio and Facebook Messenger) settings can be very different and also there are DB constraints that should be in place for those settings.

We do work around this issue with an application logic which takes params for the embedded schema (which defined as :map type on parent changeset) and validates/casts them if embedded changeset is valid or properly adds errors to the parent otherwise. Here are some code:

A function that shows how dynamic changeset works in our case:

  defp cast_provider_settings(changeset, provider_field, provider_settings_field) do
    with {:ok, provider} <- fetch_change(changeset, provider_field),
         {:ok, settings} <- fetch_change(changeset, provider_settings_field),
         provider_settings_changeset = Provider.settings_changeset(provider, settings),
         {:ok, valid_settings} <- Validator.fetch_valid_attrs(provider_settings_changeset) do
      put_change(changeset, provider_settings_field, valid_settings)
    else
      :error ->
        changeset

      {:error, :not_found} ->
        changeset

      {:error, %{valid?: false} = settings_changeset} ->
        put_embedded_error(changeset, provider_settings_field, settings_changeset)
    end
  end

Here is how you can add an error to an embedded changeset defined as map:

  defp put_embedded_error(changeset, embed_field, embedded_changeset) do
    embedded_type =
      {:embed,
       %Ecto.Embedded{
         cardinality: :one,
         field: embed_field,
         on_cast: nil,
         on_replace: :raise,
         owner: %{},
         related: Transport,
         unique: true
       }}

    %{
      changeset
      | changes: Map.put(changeset.changes, embed_field, embedded_changeset),
        types: Map.put(changeset.types, embed_field, embedded_type),
        valid?: false
    }
  end

(Notice that you can’t override types and leave embedded changeset in Ecto Schema where :map type was defined because you would get a cast error. Ecto.Changeset does use pre-compiled type information when insert happens so overriding only helps when you use functions like traverse_errors/2.)

And even if you do that, there is a lot of issues that persist here. The main one right now for us is constraints - they are lost when embedded schema turned into a map and moving them manually to parent doesn’t make sense (error field would point to a wrong direction).

Other ways to hack around:

  1. Define multiple schemas per database entity (or even combine that with PostgreSQL table inheritance). This one looks weird for me because when I fetch data back from DB I do want to see only one kind of schema. Data that I want to put there should be exactly what I get back.
  2. Do not use dynamic embeds. This option looks poor because there is sooo many use cases where dynamic embed makes perfect sense.

As a very raw suggestion how we can deal with that:

  1. We might add a :changeset type for Ecto.Schema.
  2. It’s application responsibility to actually implement logic how embedded changeset gets there, on which fields it’s resolved, etc. (I don’t think that Ecto needs to add any kind of magic here.)
  3. Repo operations should take care of changesets in :changeset fields in a same way as they would do with usual embedded schema.

OR

  1. Make Ecto use type information from changeset (removing the calls to Schema.__*__ functions) which is not straightforward and would make changesets structs much bigger. (See this issue.)

Showing Posts 24 to 15

mathieuprog

mathieuprog

I suggested to copy the type field, so you end up with the exact same structure in the DB, no difference for querying, i.e. a type and payload fields in the schema, in addition to a type in the payload to enable polymorphism. That small logic for copying the type would be done in the changeset.

Resorting to non documented, private internals such as Ecto.Embedded struct and its fields (thus that might change and break your app) seems worse as a solution. Would you go for a minor weirdness, or a fragile hack?

Indeed I don’t know about the limitations you faced back in 2018, as I wasn’t doing Elixir at that time.
I see however that ecto_poly was released end 2017, so there were some possibilities.

AndrewDryga

AndrewDryga OP

There are few reasons:

  • One is that in PostgreSQL using index on regular column and JSONb column at the same query is expensive (at least it was back when we designed embeds) - we do query data by type;
  • Back then, casting by using Ecto.Type was not flexible enough to cover our needs either;
  • For me, it just feels weird to have JSON field that holds information about itself, because type for us is strictly field even when payload is empty.
mathieuprog

mathieuprog

Why not just copy the type field into the payload? You could do that in the changeset, before calling cast of a custom type.

AndrewDryga

AndrewDryga OP

Here is our implementation: Dynamic embeds with Ecto · GitHub. It’s used in production and did not cause any issues yet. It’s very hacky though.

Notice that we explicitly call load_dynamic_embed/2 function every time schema is loaded from DB. We also dump everything to string-keyed map if changeset is valid to mimic behaviour of writing and reading from DB (so that you can compare user == inserted_user in test cases, otherwise string- and atom-keyed maps would be a problem).

If embedded changeset has an error it looks very much like nested schema was defined by using embeds_one/many`.

AndrewDryga

AndrewDryga OP

Thank you, the library looks nice. It does not fit out needs though as if JSONb column has type information it’s trivial to make it polymorphic without any external libraries (just pattern match on load in specific type).

Our cast is different, in the raw Ecto schema we have a type field and then payload field which should be casted depending on type field value. Which Ecto does not allow as you can’t access type value while casting/dumpin/loading payload field.

thojanssens1

thojanssens1

I don’t see why Ecto could be unable to load data.

I tried playing with datetimes, dates, times, decimals, … to be stored in :map, and all seem to be dumped and loaded back to the right data without issues with Postgres without these encodings/decodings.

Also, do you know what other format can be given to embedded_dump(type, value, format) other than :json?

lukaszsamson

lukaszsamson

ElixirLS Core Team
  1. If I remember correctly the issue with that approach was that dumping my structs as ecto map used default Jason.Encoder protocol implementation for decimals, dates, etc and ecto was then unable to load them correctly (was loosing data or simply crashing)
  2. You’re right, It’s more on-topic than I initially thought :wink:
thojanssens1

thojanssens1

@lukaszsamson two things

  1. I think that the Ecto.Type.embedded_dump and Ecto.Type.embedded_load in those Enum.reduce_while are unnecessary, and that represents a lot of code in your Ecto type.
    Because you know the schema based on the ‘type’ field (def load(%{@type_field => module_string} = data)), so when loading the data from the DB, you can simply cast those values against the changeset of your schema. And the cast will convert the data to the right elixir data. Or did I miss something?

  2. I don’t understand why you say off-topic as the library above for example does exactly what you are doing, i.e. picking dynamically an embedded struct based on some ‘type’ field, and store it in some field.

lukaszsamson

lukaszsamson

ElixirLS Core Team

I was working with Postgres so JSONB was there. The problem was ecto mapping a single JSON column to one of several structs depending on some other column. Using a simple map serialized to JSON I would lose all ecto goodness like decimals, dates, times, casting, loading, validatins etc.

thojanssens1

thojanssens1

Were you working with a DB that has no JSONB support? Or why did you need to convert to JSON as seen in the code?

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews