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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
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
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
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
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
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Other popular topics
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
I would like to know what is the best IDE for elixir development?
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









