leonardorame
Hi, I’m new to Elixir and Phoenix.
I’m doing some experiments with Ecto.Adapters.SQL.query and now I would like to render a query result into an HTML table.
So far I did this:
page_controller.ex:
def hello(conn, _params) do
{:ok, result} = Ecto.Adapters.SQL.query(:"Elixir.Hello.repo", "select idstudy, modality from study order by idstudy", [])
render(conn, "hello.html", studies: result)
end
hello.html.heex
<section class="phx-hero">
<h1>Hello!</h1>
<table>
<%= for study <- @studies do %>
<tr>
<td><%= study.idstudy %></td>
<td><%= study.modality %></td>
</tr>
<% end %>
</table>
</section>
When I run that code I get this:
##### Protocol.UndefinedError <small>at GET</small> <small>/hello</small>
# protocol Enumerable not implemented for %Postgrex.Result{columns: ["idstudy", "modality"], command: :select, connection_id: 4334, messages: [], num_rows: 8, rows: [[12070, "CR"], [12071, "MR"], [12072, "CR"], [12073, "MR"], [12074, "CR"], [12075, "CR"], [12076, "CR"], [12077, "MR"]]} of type Postgrex.Result (a struct)
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Postgrex.Resultis a struct that contains returned rows in:rowsfield. Additionally it will not map returned columns to their values, sofor study <- @studies, do: {study.idstudy, study.modality}will not work. You need to do mapping on your own.Any particular reason to write your queries by hand instead of using schemas or schemaless
Ecto.Query? Also why low caserepoin:"Elixir.Hello.repo"? That would make your life much easier if therepowould beRepo.leonardorame
Could you point me to an example?.
Well, I don’t see the value of ORMs in general, I prefer to write my queries by hand instead of adding an abstraction layer to do the same in a slightly different language.
I created this test by following an example I found by googling for
Ecto.Adapters.SQL.query(cannot find it right now). To fix it should I just replace “Elixir.Hello.repo” to “Elixir.Hello.Repo”?.kokolegorille
You might try something like this…
You also need to convert the resulting list of maps, to a list of your structs.
hauleth
Ecto is not ORM, it is more of the query API with changesets.
Your query can be written as:
It is not much different from your hand-crafted SQL (and I would say that the QUEL-like order that
Ecto.Queryprovides is much clearer than SQL “read from the middle, then end, and then beginning” order).That will return you just list of maps, what is what you expected from the beginning. But why stop there? Instead of always remembering to define
:selectin query and remember what is the table name, you can “predefine” it:And then you can do:
Ok, but now we need to define
Studytwice, also if we have a lot of such tables, then the code will became repetitive, so Ecto has handy “schemas” which allow you to simplify all of that:So as you see, there is nothing magical that does stuff behind your back. It is just small set of helpers to write queries faster and easier instead of manipulating strings. It also makes it much harder to accidentally have SQLi, as if you do something like:
It will send it as a prepared statement to the DB, which mean that passed argument will always be treated as properly escaped.
EDIT:
If I haven’t stated it strong enough QUEL >>>>>>>>>>>> SQL and do not even bother
@-ing me, as there is nothing to discuss.leonardorame
Great explanation, thank you! @hauleth .
leonardorame
@kokolegorille, by using
Enum.mapI end up with this:kokolegorille
It’s ok for simple scenario, but if You need associations, it would be preferable to use @hauleth solution.
benwilson512
And if you have structs, you can use Ecto.Repo — Ecto v3.14.0 to load the queried results into a struct.
kokolegorille
Even easier… thanks for the Repo.load tip