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
Ok so what I ended up doing is something like the following
fragment("((metadata->>'file')::json->'line')::text::int") seems to work 
Or what @jtranin suggested fragment("(metadata#>>'{file, line}')::integer ASC")
2
Popular in Questions
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
I would like to know what is the best IDE for elixir development?
New
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
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
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
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
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
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
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
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








