Hi everyone, i am trying to wrap my head around why the following error could appear in the test environment only:
** (DBConnection.EncodeError) Postgrex expected a binary, got %{}. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
This occures when i am trying to call Game.create_character(user, parms)
in tests.
In iex -s mix everything works as intended:
{:ok,
%Dsa.Game.Character{
__meta__: #Ecto.Schema.Metadata<:loaded, "characters">,
data: %Dsa.Game.CharacterData{},
...
}}
I defined the embeded schema like this:
defmodule Dsa.Game.Character do
schema "characters" do
embeds_one :data, CharacterData
...
end
def changeset(character, attrs) do
character
|> cast(attrs, [])
|> cast_embed(:data, with: &CharacterData.changeset/2)
end
end
defmodule Dsa.Game.CharacterData do
@primary_key false
embedded_schema do
end
def changeset(data, params) do
data
|> cast(params, [])
end
end
This is how i create the character via the context:
def create_character(%User{} = user, attrs) do
%Character{}
|> Character.changeset(attrs)
|> Ecto.Changeset.put_embed(:data, %{})
|> Ecto.Changeset.put_assoc(:user, user)
|> Repo.insert()
end
Finally, I made sure to define :data
as a map in the migration:
create table(:characters) do
add :data, :map, null: false
...
end
Any ideas why i would the the Encode Error in the test environment but not in the App?
Any help on this is much appriciated! Thanks