JohnnyCurran

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

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!

Where Next?

Popular in Questions Top

electic
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
albydarned
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
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
jerry
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
belgoros
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
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
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
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
dotdotdotPaul
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
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

Other popular topics Top

electic
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
stefanchrobot
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
aesmail
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
shijith.k
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
Qqwy
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...
3271 127536 1222
New
dokuzbir
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
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

We're in Beta

About us Mission Statement