xlive

xlive

`:any` type for embedded schemas in non-virtual fields

Hey there!

We are using Ecto to parse some API response (it works pretty well!).
We want this response to be converted to a struct, so we first do all the needed validations and at the end we do an apply_action(changeset, :parse).
To do so, we would like to have a module that defines an embedded_schema like the following (we won’t save this):

defmodule MyApp.Response do
  use Ecto.Schema

  @primary_key false
  embedded_schema do
    field(:custom_field, :any)
    ...
  end
end

The “problem” comes with this :any type. When compiling, Ecto complains that only virtual fields can be defined with the :any type.
Here the error:

== Compilation error in file lib/my_app/api/response.ex ==
** (ArgumentError) only virtual fields can have type :any, invalid type for field :custom_field
    (ecto 3.11.1) lib/ecto/schema.ex:2012: Ecto.Schema.__field__/4
    lib/my_app/api/response.ex:36: (module)
    (ecto 3.11.1) lib/ecto/schema.ex:2241: Ecto.Schema.__embeds_module__/4
    lib/my_app/api/response.ex:32: (module)

Is there an explanation of why they must be virtual?

In our use case, this field can have different types (boolean, integer, string, etc.), so we need this flexibility here.

We think that, as embedded_schemas are just saved as blobs or, like in our case, not saved at all, this doesn’t make a lot of sense. But maybe someone has a better understanding about this.

Thanks in advance! :purple_heart:

Most Liked

cevado

cevado

You can use a schemaless changeset

{%MyStruct{}, %{custom_field: :any}}
|> Ecto.Changeset.cast(%{"custom_field" => "1"}, [:custom_field])
|> Ecto.Changeset.apply_changes()
Adzz

Adzz

Ecto can work for some inputs I found I quickly outgrew it and wrote GitHub - Adzz/data_schema: Declarative schemas for data transformations. too. If you need to map data that isn’t essentially a map or a list then ecto schemas aren’t the solution imo.

sodapopcan

sodapopcan

I’m assuming this is for similar reasons as to why you can’t use nil for embeds_many—which came up again recently—that embeds are meant to be treated the same as regular relations. I think your only option if you want to keep using Ecto is to create your own type. Otherwise you could check out Drops which is a more general schema library.

cevado

cevado

But now actually answering that part, and expanding on what @sodapopcan . Understand that althought schemas and changesets are just mapping abstractions. both schema and embedded schemas are expected to be at some point dumped to a database.
Since that is not your intention, i’d recommend to use schemaless changeset(you can use that with structs or plain simple maps).
in case you need features like nesting, relations and some more complex stuff that doesn’t work with schemaless changeset i’d do something like:

@default [virtual: :true]
schema "" do
  field :custom_field1, :any, @default
  field :custom_field2, :any, @default
  ...
end
xlive

xlive

Thank you all for answering here!!
@cevado yes… Actually this variable will be inside an assoc of the main struct
I will try then to find a better way to check these types :pray:
Maybe Drop is a nice candidate! Thx @sodapopcan !!

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

Other popular topics Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement