script

script

Select merge with joins and count

I am using select merge to build the query dynamically. But i can’t understand why ecto give me these errors.
this is the query

                            Ecto.Query<from r0 in Room,
                            select: merge(f0, %{"$aggregate": %{"$count": %{^"floor" => count(f0.floor)}}})>

this is the code:

                  from(
                  r in rooms,
                 select_merge: %{"$aggregate": %{"$count": %{^field => count(field(r, ^atom_field))}}}
                 )

and I got this error:

                        struct Room `does not have the key :"$aggregate".

And if i use it in joins:

this is the query:

  from(
    h in Hospital,
    right_join: r in "rooms",
    on: h.id == r.hospital_id,
    where: r.name == ^"room 1" and ^true,
    select: merge(h, %{^:rooms => map(r, [:name, :purpose, :description])})
  )

this is the code:

       from(
      [h, ..., r] in hospital,
      select_merge: %{
        ^String_to_atom(rooms) => map(r, [:fields])
      }
    )

the error is:

     Postgrex expected a binary, got :rooms. Please make sure the value you are passing matches the 
     definition in your table or in your query or convert the value accordingly.

If i change the rooms to string it will throw the same error i am getting in the count query.

May be I am missing something very obvious here.

Any help will be much appreciated

Thanks

Most Liked

LostKobrakai

LostKobrakai

If you use from x in Schema ecto will implicitly expect you want …, select: x. This will return a list of %Schema{} structs, so each field you select must be part of the struct.

script

script

Thanks for your reply.
I figured out the problem.
In the select merge in the last paragraph . It states that you have to supply the map or struct to merge on. If you supply struct it’s value must be present in the key in the merge map. Thats why i am getting these errors.

By supplying an empty map or selecting fields from the struct and then pass them to select merge solved the problem.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement