UnderFenex

UnderFenex

Ecto Query as Map and All fields

Hi, is there a way to get the query in a map with all its fields and not declare it one by one:

select: map(q, [:id, :statement])

  def show(conn, %{"id" => chapter_id}) do
    questionsQuery = from q in Question, where: q.chapter_id == ^chapter_id, select: map(q, [:id, :statement])
    questions = Poison.encode!(Repo.all(questionsQuery))
    render(conn, "show.html",  questions: questions)
  end

Most Liked

LostKobrakai

LostKobrakai

You can ask the schema for all its fields.

fields = Sensor.__schema__(:fields)

…,
select: map(sendor, ^fields)
Qqwy

Qqwy

TypeCheck Core Team

Isn’t the default that is returned when not using select a struct which contains all the fields? If you want to remove the struct name, run Map.from_struct/1 on it afterwards.

nTraum

nTraum

I am not sure if necroing this thread is okay, but I think I have the very same question as OP, but the answer from @Qqwy does not satisfy my needs.

My use case is that I want to query some data from my repo. The data is basically just a defined schema, plus some additional fields coming from a join with another table.
Yes I could implement virtual fields, but it feels a bit weird polluting the general schema definition for a one-off join in one of my queries.

Therefore my idea was to simply select the struct as a map and select_merge additional key-value pairs on top of it.

Here’s the schema definition and my attempted Ecto Query, which is not supported on ecto 3.11.1:

  schema "sensors" do
    field :name, :string
    has_many :readings, Reading
  end

  schema "readings" do
    field :name, :string
    field :value, :float
    field :taken_at, :utc_datetime
    belongs_to :sensor, Sensor
  end

  from(sensor in Sensor,
    join: reading in assoc(sensor, :readings),
    # This API does not work, ** (Ecto.Query.CompileError) `map()` is not a valid query expression.
    select: map(sensor),
    select_merge: %{taken_at: reading.taken_at, value: reading.taken_at}
  )

Of course I could manually select all fields from sensors, but that seems cumbersome and easy to break when I add more fields to the sensors schema later. Maybe there is a better way of dealing with the use case of returning data from joined tables.

Where Next?

Popular in Questions Top

9mm
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
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
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement