Nobylcher

Nobylcher

How to compare a fragment with a query reference variable in ecto?

So, let’s say I have a model Car, and a model Person, with a structures:

%Car{id: "123"}

%Person{
  options: "{"car_id":"123"}"
}

options being a text field on the underlying DB.
To fetch that from the database I can use a fragment like:
"JSON_EXTRACT(?, \"$.car_id\")"

It works when using the order_by statement for example, however, when fetching all Persons and joining all the related Cars based on that fragment, it doesn’t…

I tried this, and put that return value on the select statement to try to debug:

 from p in Person, 
    join: c in Car, on: c.id == fragment("JSON_EXTRACT(?, \"$.car_id\")" p.options),
    select: {c.id == fragment("JSON_EXTRACT(?, \"$.car_id\")" p.options), fragment("JSON_EXTRACT(?, \"$.car_id\")" p.options), c.id}

which basically returns [{false, "123", "123"}] (false on every comparison)

I would be grateful if somebody had a hint for this.

Marked As Solved

Nobylcher

Nobylcher

using UNHEX(JSON_UNQUOTE(JSON_EXTRACT(...))) solved the problem.

Also Liked

idi527

idi527

:wave:

Are you using MySQL? I couldn’t find JSON_EXTRACT function in Postgres …


Judging by https://stackoverflow.com/questions/39818296/using-mysql-json-field-to-join-on-a-table, you might need to use JSON_UNQUOTE in the join expression like

from p in Person, 
    join: c in Car, on: c.id == fragment("JSON_UNQUOTE(JSON_EXTRACT(?, \"$.car_id\"))" p.options),
    select: {c.id == fragment("JSON_UNQUOTE(JSON_EXTRACT(?, \"$.car_id\"))" p.options), fragment("JSON_EXTRACT(?, \"$.car_id\")" p.options), c.id}

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
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
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

Other popular topics Top

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...
3268 119930 1237
New
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 45766 226
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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
fayddelight
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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 Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement