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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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 ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement