vasspilka

vasspilka

Ecto nested embeds with jsonb

Hello, I am having some trouble with nested embeds, I have a jsonb column metadata which looks like this:

  field(:collected_from, :string)
  field(:era, :string)

  embeds_one(:file, File)
  embeds_one(:sync, Sync)
end

But when try to get something from file I cannot, it looks like the File embed is saved as string.
Repo.all(from d in "data", select: fragment("metadata->>'file'")) This returns something like:

"{\"id\": \"63769d9b-fee8-456d-afae-96e8a9efaaa7\", \"line\": 4, \"path\": \"/home/x/Work/EPark/eparkomat/_build/dev/lib/eparkomat/priv/mock_folder/labels/6219321040015.MOV.txt\", \"file_created_time\": \"2018-03-26T12:33:18\", \"file_accessed_time\": \"2018-05-07T10:26:58\", \"file_modified_time\": \"2018-03-26T12:33:18\"}",
"{\"id\": \"9420931f-afa6-42d7-b5b7-7a4bd42a670e\", \"line\": 9, \"path\": \"/home/x/Work/EPark/eparkomat/_build/dev/lib/eparkomat/priv/mock_folder/labels/6219321040015.MOV.txt\", \"file_created_time\": \"2018-03-26T12:33:18\", \"file_accessed_time\": \"2018-05-07T10:26:58\", \"file_modified_time\": \"2018-03-26T12:33:18\"}",
"{\"id\": \"2a362e2f-9eb4-4462-bff6-42f21e8e47e4\", \"line\": 3, \"path\": \"/home/x/Work/EPark/eparkomat/_build/dev/lib/eparkomat/priv/mock_folder/labels/8689819590011.MOV.txt\", \"file_created_time\": \"2018-05-07T10:25:19\", \"file_accessed_time\": \"2018-05-07T10:25:19\", \"file_modified_time\": \"2018-05-07T10:25:19\"}"]

But when I do Repo.all(from d in "data", select: fragment("metadata->>'file.line'")) it just returns nils

Any ideas?

Marked As Solved

vasspilka

vasspilka

Ok so what I ended up doing is something like the following

fragment("((metadata->>'file')::json->'line')::text::int") seems to work :slight_smile:

Or what @jtranin suggested fragment("(metadata#>>'{file, line}')::integer ASC")

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
siddhant3030
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

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement