unlogic

unlogic

Question about Ecto dynamic and sortby

I’m trying to add a sort feature to an API which allows the user to pass the field name to sort by. I figured I could use dynamics as per https://hexdocs.pm/ecto/dynamic-queries.html#building-dynamic-queries but I run into an issue when adding the sort order specifier. this works fine:

dynamic([q], field(q, ^String.to_atom(sortby)))

this however gives an error

dynamic([q], desc: field(q, ^String.to_atom(sortby)))

** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same size

In order to get this working now I’ve used order_by instead of dynamic

order_by(query, [q], desc: field(q, ^String.to_atom(sortby)))

Which works, but I’d like to understand why the dynamic isn’t working for me, as it’s pretty much the same as in the documentation

Marked As Solved

LostKobrakai

LostKobrakai

So the problem is that the keyword syntax is special to the order_by macro. dynamic allows you to capture sql (query…) expressions. desc: field is not a query expression on it’s own, though. The keyword syntax is just a convenience to use elixir syntax with order_by. If you need to put the direction in the dynamic use plain sql possibly with an fragment.

def dynamic_order_expr(field, :asc), 
  do: dynamic([q], fragment("? ASC", field(q, ^field)))

def dynamic_order_expr(field, :desc), 
  do: dynamic([q], fragment("? DESC", field(q, ^field)))

Also Liked

LostKobrakai

LostKobrakai

Oh, that’s interesting. Maybe this is actually supposed to work (which I wouldn’t have expected). Please open an issue on ectos github, so either docs or the bug can be fixed.

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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
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
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

We're in Beta

About us Mission Statement