JohnnyCurran
Ecto - Why doesn't `Jason.encode` work for interpolating in `jsonb` queries?
I cannot figure out why this test is failing. It fails on assert result_one == result_two. The hardcoded string sigil query returns a list of length 1, the interpolated query returns an empty list.
The Jason.encode’d struct is apparently the same as the hardcoded string sigil.
The schema of Job isn’t that important, just that custom_fields is a jsonb column.
test "json" do
intern = %{"Employment Type" => "Intern"}
insert(:job, custom_fields: intern)
intern_json = Jason.encode!(intern)
assert intern_json === ~s|{"Employment Type":"Intern"}| # This passes fine
query =
from j in Job,
as: :job,
where: fragment("custom_fields @> ?", ^intern_json),
select: j.id
query_two =
from j in Job,
where: fragment("custom_fields @> ?", ~s|{"Employment Type":"Intern"}|),
select: j.id
result_one = Repo.all(query)
result_two = Repo.all(query_two)
assert result_one == result_two # Failure here
end
Result:
Assertion with == failed
code: assert result_one == result_two
left: []
right: ["job_id0"]
Thanks for all the help
Marked As Solved
JohnnyCurran
Okay, figured it out:
Instead of using Jason.encode, use type/2 with :map:
intern = %{"Employment Type" => "Intern"}
insert(:job, custom_fields: intern)
query =
from j in Job,
as: :job,
where: fragment("custom_fields @> ?::jsonb", type(^intern, :map)),
select: j.id
Thanks!
3
Popular in Questions
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
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
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
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
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
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
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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
Other popular topics
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









