Sathras

Sathras

Ecto Error with embeded schemas in test environment but not in app

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

Marked As Solved

Sathras

Sathras

Oh i am stupid! I forgot to migrate the test environment:
MIX_ENV=test mix ecto.reset

All resolved now :slight_smile:

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement