Sanjibukai
How to fetch data field between 2 models from 2 Context Schema (that share the same Table)
Hello everybody!
I love how Phoenix (with Ecto) allows clean boundaries thanks to context and schemas.
But I just came across something that seems trivial yet I’m not able to find how to manage that..
The question might be confusing so here is a quick example..
Let’s say I have one database table about cars..
And I have two contexts one for the sales and one for the mechanics.
So Sales.Car deal with sales related data like price etc. while Mech.Car deal with technical datas and metrics about the car..
Each Schema on each Context having their own changeset it’s simple and clean etc.
However, now I want to display somewhere like in a Dashboard, data from both context/schemas…
Let’s say for example Sales.Car.price and Mech.Car.mileage..
If I already have access to one Model, e.g Sales.Car in one of my controller (which I fetched from the database).. Now how can I have access to the data from the other context?
This seems really trivial, but yet I have no clue how to do that..
If it was two different tables, I have put a relation like CarMetrics belongs to Car (which CarMetrics would had the car_id), and I would have fetch the Car with the CarMetrics preloaded.
However, here there’s no key to reference..
One easy solution could be to fetch from the other context using the ID of my previous fetch..
sales_car = Sales.get_car(..)
metrics_car = Mech.get_car(sales_car.id)
But here, there will be two database query right..
So is it possible to have access to those columns with one query?
Thank you for any suggestions..
Most Liked
pmangalakader
You can use the ecto joins clause combined with a where match clause.
Here’s an article, that details it out: Please feel free to skip ahead to InnerJoins section in the following:
Official link:
It’s easier to add a reference key in your relationship table to make this simpler, but I’m not aware of the intricate details of your implementation.
pmangalakader
Even though they are different contexts, you can access using alias in the other context for using them in your join clause.
Your query should be like in your Sales Context file:
alias Mech.Car
q = from s in Sales.Car,
joins: m in Mech.Car,
where: s.id == m.sales_id,
select: %{price: s.price, mileage: m.mileage}
Repo.all(q)
Sanjibukai
Thanks.. I’ll try that!
Popular in Questions
Other popular topics
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









