Terbium-135

Terbium-135

SQL to ecto: custom order by getting (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "AS"

What am I missing?
I am trying to ‘translate’ following SQL query to ecto:

SELECT g.groupname, g.id FROM groups AS g
LEFT OUTER JOIN unnest ('{class3, class1, class11, latecomer}'::text[]) WITH ORDINALITY sort(sort_order, ord)
ON g.groupname = sort_order
ORDER BY ord

Result:

My try using ecto:

from g in Backend.Schema.Group,
  left_join:
    sort in fragment(
      "unnest ('{kindergarden, class1, class2, class3, class4}'::text[]) WITH ORDINALITY sort(name, ord)"
    ),
  on: g.groupname == sort.name,
  order_by: sort.ord,
  select: {g.groupname, g.id}

Gives the following:

** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "AS"

    query: SELECT g0."groupname", g0."id" FROM "groups" AS g0
 LEFT OUTER JOIN unnest ('{kindergarden, class1, class2, class3, class4}'::text[]) WITH ORDINALITY sort(name, ord) AS f1
 ON g0."groupname" = f1."name" ORDER BY f1."ord"

I tried to use as: :sort but this generates the same query…

Marked As Solved

zenchild

zenchild

This isn’t exactly what you are doing, but @LostKobrakai has a post on using unnest for sorting that you might want to check out and see if it unblocks you.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement