@derive Jason.Encoder versus Inline embedded schema (ecto)

I would like to Jason.encode! an embedded schema that has an inline embedded schema.
I can annotate the outer (parent) embedded schema, but not the inline embedded schema (child).
This compiles but will not encode because of the child:

@primary_key false
@derive Jason.Encoder
embedded_schema do
  field :parent_name, :string
  embeds_many :children, Child, primary_key: false do
    field :child_name, :string
  end
end

Adding @derive in the same way as @primary_key does not work:

  embeds_many :children, Child, primary_key: false, derive: Jason.Encoder do

** (ArgumentError) invalid option :derive for embeds_many/3

What is the best way to work around this?

You want to move @derive inside.

@primary_key false
@derive Jason.Encoder
embedded_schema do
  field :parent_name, :string
  embeds_many :children, Child, primary_key: false do
    @derive Jason.Encoder
    field :child_name, :string
  end
end

7 Likes

That simple… why didn’t I think of that. Tnx!

2 Likes