(Ecto.CastError) expected params to be a :map, got: `%Hi.Msg.Translations{}`

I’m trying to use library trans.

This my Schema module:

defmodule Hi.Msg do
  use Ecto.Schema
  use Trans, translates: [:msg],
    default_locale: Application.compile_env(:hello, :locale)

  import Ecto.Changeset

  schema "msgs" do
    field :msg, :string

    embeds_one :translations, Translations, on_replace: :update, primary_key: false do
      embeds_one :en, Hi.Msg.Translation
      embeds_one :ru, Hi.Msg.Translation
      embeds_one :uz, Hi.Msg.Translation
    end
  end

  def changeset(msg, params \\ %{}) do
    msg
    |> cast(params, [:msg])
    |> cast_embed(:translations, with: &translations_changeset/2)
    |> validate_required([:msg])
  end

  defp translations_changeset(translations, params) do
    translations
    |> cast(params, [])
    |> cast_embed(:en)
    |> cast_embed(:ru)
    |> cast_embed(:uz)
  end

  def translate(msgid) do
    import Ecto.Query
    msg =
      Hi.Msg
      |> Repo.get_by!(msg: msgid)

    Trans.Translator.translate(msg, :msg,
      Application.get_env(:hello, :locale))
  end
end

defmodule Hi.Msg.Translation do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  embedded_schema do
    field :msg, :string
  end

  def changeset(fields, params) do
    fields
    |> cast(params, [:msg])
    |> validate_required([:msg])
  end
end

This is my migration:

defmodule Msg.Repo.Migrations.CreateMsg do
  use Ecto.Migration

  def change do
    create table(:msgs) do
      add :msg, :string
      add :translations, :map
    end

    create unique_index(:msgs, :msg)
  end
end

When i’m trying to create through changeset() new %Hi.Msg{} with %Hi.Msg.Translations{} inside it, this error occurs:

iex(1)> alias Msg.Repo
iex(2)> alias Hi.Msg
iex(3)> alias Hi.Msg.{Translation, Translations}

iex(6)> %Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %Translations{}})
 
** (Ecto.CastError) expected params to be a :map, got: `%Hi.Msg.Translations{en: nil, ru: nil, uz: nil}`
    (ecto 3.9.4) lib/ecto/changeset.ex:498: Ecto.Changeset.cast/4
    (hello 0.1.0) lib/hi/msg.ex:27: Hi.Msg.translations_changeset/2
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:137: Ecto.Changeset.Relation.do_cast/6
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:335: Ecto.Changeset.Relation.single_change/5
    (ecto 3.9.4) lib/ecto/changeset/relation.ex:121: Ecto.Changeset.Relation.cast/5
    (ecto 3.9.4) lib/ecto/changeset.ex:832: Ecto.Changeset.cast_relation/4
    (hello 0.1.0) lib/hi/msg.ex:21: Hi.Msg.changeset/2
iex(6)> 

Without Translations it works fine:

iex(5)> %Msg{} |> Msg.changeset(%{msg: "Hello!"})
#Ecto.Changeset<
  action: nil,
  changes: %{msg: "Hello!"},
  errors: [],
  data: #Hi.Msg<>,
  valid?: true
>

Schema module was copied from hex page of library trans.

I’m never used before embedded schemas, can u help me to understand this problem?

Looking at Ecto’s Embedded Schemas docs, the container changeset takes a plain map and not the embedded struct.

# where profile is an embedded schema within user
changeset = User.changeset(%User{}, %{profile: %{online: true, visibility: :public}})

which would explain your error message:

expected params to be a :map, got: %Hi.Msg.Translations{...}

So instead of: %Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %Translations{}})
Give this a try: %Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %{}})

Ok, I think I figured it out. In first argument of changeset() i’m using my struct with struct inside, but in second argument instead :struct, i need to pass :map with map inside