seymores

seymores

Querying embeds_many no result with interpolation

I have a embeds_many field in Postgresql jsonb and I am trying to query it using fragment like this:

 def find(key, val) do
     q = from p in Person,
           where: fragment("meta_tags @> ?", ^"'[{\"#{key}\":\"#{val}\"}]'"),
           select: p
     Repo.all(q)
 end

I don’t understand why hardcoded value in the fragment("meta_tags @> '[{"type":"test"}]'") works but will return no result once I use interpolated values from the function input.

The logs are as below:

Not returning result with interpolated string.

[debug] QUERY OK source="world_persons" db=15.1ms
SELECT w0."id", w0."name", w0."salutation", w0."original_name", w0."gender", w0."dob" w0."email", w0."active", w0."inserted_at", w0."updated_at" FROM "persons" AS w0 WHERE (meta_tags @> $1) ["'[{\"type\":\"test\"}]'"]
[]

With results when I hardcode the value.

[debug] QUERY OK source="world_persons" db=5.2ms decode=0.1ms
SELECT w0."id", w0."name", w0."salutation", w0."original_name", w0."gender", w0."dob", w0."email", w0."active", w0."inserted_at", w0."updated_at" FROM "persons" AS w0 WHERE (meta_tags @> '[{"type":"test"}]') []
(... results)

This won’t work with hardcoded value

where: fragment("meta_tags @> ?", ^"[{\"type\":\"test\"}]"),

This works without the interpolated operator

where: fragment("meta_tags @> ?", "[{\"type\":\"test\"}]"),

From a suggestion in stackoverflow, I cast the query to text and then to jsonb and that works.

where: fragment("meta_tags @> ?::text::jsonb", ^"[{\"#{key}\":\"#{val}\"}]"),

What have I missed in this query to make this work without the casting trick?

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement