msyavuz

msyavuz

Ecto schema inside its own context?

Hello, i am a newbie in both phoenix and elixir and its been a blast so far. I have a question about ecto schemas and phoenix contexts.

Suppose i have two tables called clubs and club_members. While generating a new schema with phx.gen.schema

  1. Should i create the club schema under club context ?

  2. Or should i create club schema without context and put member schema under club context?

First approach results in module name being Club.Club but directory structure seems better. While module naming in second approach seems better but club schema sits outside club directory.

I guess what i am asking is which one of these is better?

/001   club/  member.ex
/002   club.ex
/001   club/  member.ex
/001   club/  club.ex

Marked As Solved

sodapopcan

sodapopcan

Generally if you are using contexts as a boundary, then you put all the schemas belonging to a context under it.
Also by convention, if there isn’t a good unique name for a context (which there often isn’t) then it’s the pluralized version of its primary schema. You could just call your context Club if you want, but this can become confusing and it also makes it impossible to alias the schema within the context as they will have the same name. So the “normal” thing to do is this:

lib/my_app/clubs/club.ex
lib/my_app/clubs/member.ex
lib/my_app/clubs.ex

This way the directory structure mimics the module structure. Some people like to put the context file in the context directory, which is fine, whatever floats your boat!

Technically, though, you are free to do whatever you want! The generators push us towards certain conventions, but otherwise Phoenix doesn’t impose any conventions on us.

Also Liked

zachallaun

zachallaun

The most common way to solve this would be to call the context clubs, so you would have Clubs.Club and Clubs.Member.

msyavuz

msyavuz

Thank you for the explanation!

Last Post!

msyavuz

msyavuz

Thank you for the explanation!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement