Getting field names together with values in select queries for specific fields

395 views
Skip to first unread message

Kofi Boakye

unread,
Nov 24, 2014, 1:09:29 AM11/24/14
to elixi...@googlegroups.com
Hi all,
Is there a way to return the column name and the value as a map when making select queries for specific fields? An example is below :

   from(c in UpshsMgt.AuthUser,
      where: c.username=="kofi" ,
         select: [c.username,c.id] )
     |>  UpshsMgt.Repo.all

The results i get : [["kofi", 1]]

Is there a way to get back a struct or a map such as [[username: "kofi", id: 1]] ?

Thanks in advance for any help or suggestions.

Kofi Boakye

unread,
Nov 24, 2014, 1:18:51 AM11/24/14
to elixi...@googlegroups.com
hah!! just noticed my error..uhmm slight correction.. i meant a map such as %{username: "kofi",id: 1}  or a list as above!! morning bleariness!!

Eric Meadows-Jönsson

unread,
Nov 24, 2014, 7:26:01 AM11/24/14
to Kofi Boakye, elixi...@googlegroups.com

select: [username: c.username, id: c.id] should work. Support for maps in select is planned but not in yet.


--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Eric Meadows-Jönsson

Kofi Boakye

unread,
Nov 24, 2014, 10:01:40 AM11/24/14
to elixi...@googlegroups.com, kof...@gmail.com

Hi Eric,
Thanks for the reply. However i tried it and still doesnt work.

I run this

from(c in UpshsMgt.AuthUser, select: [username: c.username ,id: c.id]) |> UpshsMgt.Repo.all


and the error message is below.

** (Ecto.QueryError) `:username` is not a valid query expression
     
(ecto) lib/ecto/query/builder.ex:106: Ecto.Query.Builder.escape/3
   
(elixir) lib/enum.ex:1036: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
     
(ecto) lib/ecto/query/builder/select.ex:41: Ecto.Query.Builder.Select.do_escape/3
   
(elixir) lib/enum.ex:1036: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
     
(ecto) lib/ecto/query/builder/select.ex:104: Ecto.Query.Builder.Select.build/4
     
(ecto) expanding macro: Ecto.Query.select/3
   
(elixir) iex:19: :elixir_compiler.__FILE__/2
     
(ecto) expanding macro: Ecto.Query.from/2

Reply all
Reply to author
Forward
0 new messages