valyukov

valyukov

Dynamic fields in Ecto queries

Hi there,

How I can build queries with dynamic fields assignment in Ecto?
I try to do next thing:

import Ecto.Query
field = :id
value = 1

from(o in Order, where: o[field] != ^value)

** (Ecto.Query.CompileError) `o[field]` is not a valid query expression
    (ecto) expanding macro: Ecto.Query.where/3
           iex:6: (file)
    (ecto) expanding macro: Ecto.Query.from/2
           iex:6: (file)

how you can see it doesn’t work, but who cares? keyword syntax allow to do next things:

from(o in Order, where: ^[{field, value}])

and this works fine, but only for simple cases, it doesn’t allow neglect syntax (!=), like, is not, etc.

I don’t give up, and try to apply some metaprogramming magic:

(quote do: unquote(Order) |> where([q], q.unquote(field) == ^unquote(var!(value)))) |> Code.eval_quoted |> elem(0)

And it works perfect, however I need to make join with dynamic association, the next example throw exception and I have no idea how to solve it:

assoc = :transaction

(quote do: unquote(Order |> Ecto.Queryable.to_query) |> join(:inner, [r], l in assoc(r, unquote(var!(assoc)))) |> where([r, l], l.unquote(field) == ^unquote(var!(value)))) 
|> Code.eval_quoted 
|> elem(0)
** (CompileError) nofile: invalid quoted expression: #Ecto.Query<from o in Store.Order>
      (ecto) expanding macro: Ecto.Query.where/3
             nofile:1: (file)
    (elixir) expanding macro: Kernel.|>/2
             nofile:1: (file)

but if I quote module all works fine

how we see there is only one problem that join macros throw error when I try to pass Ecto.Query struct to it. Next snippet works well, but in my app I would like to pass exactly Ecto.Query to join.

assoc = :transaction

(quote do: unquote(Order) |> join(:inner, [r], l in assoc(r, unquote(var!(assoc)))) |> where([r, l], l.unquote(field) == ^unquote(var!(value)))) 
|> Code.eval_quoted 
|> elem(0)

Schemes:

defmodule Order do
  use Ecto.Schema
  schema "orders" do
    has_one :transaction, Transaction
  end
end

defmodule Transaction do
  use Ecto.Schema

  schema "transactions" do
    belongs_to :order, Order
  end
end

Data:
orders: [{id: 1}, {id: 2}]
transactions: [{id: 1, order_id: 1}, {id: 2, order_id: 2}]

Marked As Solved

Linuus

Linuus

To use dynamic fields you can use the field macro.

thing = :points
from a in Foo, where: field(a, ^thing) > 8
19
Post #1

Also Liked

valyukov

valyukov

It works perfect! Thank you!

mosiac05

mosiac05

Thanks, works fine.

Last Post!

mosiac05

mosiac05

Thanks, works fine.

Where Next?

Popular in Questions Top

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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement