My own module for a virtual field in Ecto -- error

Why can’t I do this:

  defmodule M1 do
    defstruct [:a, :b, :c, :d]
    # other stuff
  end


  # ...............

  schema "my_model" do
    # .................
    field :my_field, M1, virtual: true 

Error

      ** (ArgumentError) invalid or unknown type MyApp.M1 for field :my_field

The module M1 is resolved but why is it disallowed to use it?

2 Likes

Have a look at the docs on custom Ecto types and see if that helps:

Ecto.Type

(Edit) Actually, this would only be relevant for persisted fields, not virtual fields.

1 Like

Then how can that help me?

1 Like

You can optionally not type check virtual fields by declaring as type :any

Does that help?

[Edit] You could also define and use a custom Ecto.Type per my previous link, but I don’t see what the benefit would be. Of course, if it were not a virtual field, you would have to define a custom Ecto.Type.

1 Like