I have a class Company
(Echo.Schema
)
defmodule MyProject.Company do
use Ecto.Schema
...
@derive {Jason.Encoder, only: [:name]}
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "companies" do
field(:name, :string)
field(:count, :integer, virtual: true)
has_many(:car, Car, foreign_key: :company_id, references: :id)
timestamps()
end
...
end
When I try to convert a Company
to a Map
, it triggers this error:
company |> Map.from_struct()
[error] GenServer #PID<0.820.0> terminating
** (RuntimeError) cannot encode metadata from the :__meta__ field for MyProject.Fleet to JSON. This metadata is used internally by Ecto and should never be exposed externally.
You can either map the schemas to remove the :__meta__ field before encoding to JSON, or explicit list the JSON fields in your schema:
defmodule MyProject.Fleet do
# ...
@derive {Jason.Encoder, only: [:name, :title, ...]}
schema ... do
...
I don’t understand the error since @derive
is set in the model ? Did I forgot to do something ?
I have set Jason as Phoenix JSON parsing lib in config/config.ex
:
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason