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
Oh i am stupid! I forgot to migrate the test environment:
MIX_ENV=test mix ecto.reset
All resolved now ![]()
Popular in Questions
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
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
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
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
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
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
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
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
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
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
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
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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...
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









