Vovchikan
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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
codeanpeace
Looking at Ecto’s Embedded Schemas docs, the container changeset takes a plain map and not the embedded struct.
which would explain your error message:
So instead of:
%Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %Translations{}})Give this a try:
%Msg{} |> Msg.changeset(%{msg: "Hello!", translations: %{}})Last Post!
Vovchikan
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:mapwith map inside