AUGERClement
I am currently trying to add a route to my JSON API which will have to return content from 2 tables in a response. From what I understand in SQL, it should be a job for a JOIN (INNER JOIN I think).
The two problems I met are the following :
- My INNER JOIN query does appear in log, but the columns fron the second table aren’t returned. And doing a SELECT of the two whole table seem tedious and error prone.
- I don’t know how shoudl I add a new render pattern in my views, and a lot of things I tried didn’t work.
Here are my codes
query = from exam in Examen,
join: patient in Patient,
on: patient.id == exam.patient_id,
where: exam.day == ^day and exam.modality == ^tag
Repo.all(query)
[debug] QUERY OK source="examens" db=10.3ms decode=1.1ms queue=3.2ms idle=764.2ms
SELECT e0."id", e0."day", e0."modality", e0."patient_id", e0."inserted_at", e0."updated_at" FROM "examens" AS e0 INNER JOIN "patients" AS p1 ON p1."id" = e0."patient_id" WHERE ((e0."day" = $1) AND (e0."modality" = $2)) ["08332022", "DICOMSTUFP"]
Controller
#This pattern use a inner join
def index(conn, %{"day" => day, "tag" => tag}) do
examens = Examens.list_examens_by_day_and_tag(day, tag)
render(conn, "index.json", examens_with_patients: examens)
end
View
defmodule WorklistWeb.ExamenView do
use WorklistWeb, :view
alias WorklistWeb.ExamenView
# Render for INNER JOIN examens x patients
def render("index.json", %{examens_with_patients: examens}) do
IO.inspect("Rendered Index")
%{data: render_many(examens, ExamenView, "examen_with_patient.json")}
end
def render("show.json", %{examen_with_patient: examen}) do
%{data: render_one(examen, ExamenView, "examen_with_patient.json")}
end
def render("index.json", %{examens: examens}) do
%{data: render_many(examens, ExamenView, "examen.json")}
end
def render("show.json", %{examen: examen}) do
%{data: render_one(examen, ExamenView, "examen.json")}
end
def render("examen_with_patient.json", %{examen_with_patient: examen}) do
%{
id: examen.id,
modality: examen.modality,
day: examen.day,
patient_id: examen.patient_id,
nom: examen.patient.nom,
prenom: examen.patient.prenom,
birthday: examen.patient.birthday
}
end
def render("examen.json", %{examen: examen}) do
%{
id: examen.id,
modality: examen.modality,
day: examen.day,
patient_id: examen.patient_id
}
end
end
The desired behavior, considering a table patient with the following fields (id, fname, lname, bday) would be
to have my query return records contain at least (id, day, modality, fname, lname, bday), and then a render fonction printing those in a json (without losing my other render patterns which are used by others controller patterns)
What are your thoughts ?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
You need to do a preload, not a join…
…or use a select I guess.
AUGERClement
I read about preload, and assoc, but it need a “belong_to”, which I didn’t have in my “Examen” schema.
I used a generator to create my schema, and used the flag for a foreign key, so My schema is like that
I’m a bit scared of just breaking everything if I add a constraint for an association, and I thought the references was already bringing enough informations.
If I use preload instead of join, it end like that
And sure, I can use a select, so if it is too tedious to add said constraint, I’ll do that, but I want to try the right way.
vrcca
You can reuse the results of the join in the preload:
AUGERClement
I still don’t get it. The preload can supposedly work with my join as argument, but I still get an error about a inexistant field :
AUGERClement
Also, I tried adding a "belongs_to in my Schema, but couldn’t make it work either
kokolegorille
You need to remove this…
Because of this…
As You cannot use both at the same time.
03juan
They’re able to co-exist when setting
define_field: falseon the belongs_to assoc. This allows for setting column-specific configuration on thefieldcall thatbelongs_todoes not have, such as:autogenerateor:redactIn this case,
is the same as
kokolegorille
Oh, nice to know… I have never used define_field: false
AUGERClement
For those who think this topic can help them :
It seems that “belongs_to” will autoname itself “patient_id”, and replacing the field in the schema by the belongs_to relation doesn’t break anything else (as it is juste a mapping)
AUGERClement
Made it work. To synthetise everything, here are the step I followed. I still advice to read the whole thread, as there is good to know informations in every response. Anyways.
field :patient_id, :idin my Examen Schema by abelongs_to :patient, Patientpreload: [patient: patient]in my Ecto query under the “where” lineThe others modifications proposed weren’t saved, but were still very useful to understand more about Ecto in general.
There is still work for the view. I’ll update this message as soon as I make progress.
UPDATE : I made the view work. My module look like this :
There is room for improvement, and I think I’ll put the examen_with_patient stuff in another view, but I reached my desired result. Marking this post as solution.
SECOND UPDATE : Forgot about this detail : " Note the controller name (
HelloController), the view name (HelloView), and the template directory (hello) all follow the same naming convention and are named after each other." from the Request Life Style Guide of the documentation.