jonbirke0927

jonbirke0927

Best practices for defining Ecto associations

Hello everyone,

My team and I are looking for perspectives on best-practices when defining associations in Ecto. Currently, we are using a Postgres database, and have defined a number of schemas in Ecto, some of which have associations. Our current pattern has been to use the Ecto.Query Macro API to define queries. When performing joins across tables / schemas, we don’t use our defined associations, and instead elect to join using the table name and an :on query expression.

Our questions are:

  • Since we are not using associations in our queries, should we eliminate the association definitions altogether?
  • Is there a benefit to keeping the unused association definitions, perhaps as documentation of underlying foreign keys?
  • Is there added benefit to using the associations when defining Ecto queries?

Thanks in advance!

Most Liked

stefanchrobot

stefanchrobot

I find associations useful when preloading data and doing joins:

defmodule Account do
  schema do
    has_many :users, User
  end
end

defmodule User do
  schema do
    belongs_to :account, Account
  end
end

and then:

user = Repo.get(User, 1) |> Repo.preload(:account)

or

from user in User,
  join: account in assoc(user, :account),
  where: account.foo = ...
al2o3cr

al2o3cr

Offhand, here’s a few:

  • filtering with the where option
  • composing associations with the through option
  • Repo.preload etc

Counter-question: what’s the advantage to doing this? It seems mostly harmless - foreign keys don’t change names very much, reducing the risk of duplication - but it’s still repeating things that don’t need to be repeated…

c4710n

c4710n

  • Answer 1: Yes. But I prefer using them.
  • Answer 2: Yes. I think it’s better to maintain association at schema level. Generally, migrations are broken code snippets, they can’t give you a panoramic view.
  • Answer 3: Yes. Read more in following content.

Suppose A has many B:

  • When you just want to get B, use Ecto.assoc.
  • When you want A and related B, use Ecto.preload or Ecto.Query.preload.
  • When :join is required, try assoc which make use of the associations defined in schema. Then, generally, you don’t need to use :on.

If you don’t define associations at schema level, you can’t use all of these good stuff.

Where Next?

Popular in Discussions Top

sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement