kalkatfyodor

kalkatfyodor

Context module name and Ecto schema module name for more than one word database tables

Normally, for a simple one word table, I would be using something like this:

$ mix phx.gen.context Cars Car cars model:string price:string

But what about if I want to have a table called cars_all and cars_from_alternative_universe?

How would the mix phx.gen.context look like for these two cases?

Marked As Solved

John-Goff

John-Goff

Alright I’ll bite. So first off, let’s just establish that one single context can have more than one schema inside of it. So if you already have

mix phx.gen.context Cars Car car....

Now your application needs to deal with cars that come from an alternate universe, which have enough different details that you want to keep them in a different table, but they’re similar enough to our dimensional cars that you still want to use the Cars module to manage them. You could then do

mix phx.gen.context Cars CarFromAlternativeUniverse cars_from_alternative_universe...

OK, so what about your other example? Let’s say you now need to track who bought your cars, you could do

mix phx.gen.context Purchaser ManWomanChild men_women_children...

Now it’s also possible to have two schemas in two different contexts refer to the same database table. Let’s say our car application also wants to know, in addition to who purchased the car, who is the riders in the car for any given trip. We could create a Riders context to handle all the logic around tracking riders, and then do

mix phx.gen.context Riders ManWomanChild men_women_children...

This now creates a new context which has a schema which refers to the same table that we created before. You can have some of the fields on the table exposed in the Purchaser schema, and some in the Riders schema. Your schema doesn’t need to have all of the fields that the table has. You can also safely refer to the same database fields from both the Riders and Purchaser schema.

Hope this helps! I’m still not certain on what exactly database structure you had in mind, so I’m not sure where cars_all would fit in, but hopefully you can see how the schema, context, and database all relate from these examples.

Also Liked

kalkatfyodor

kalkatfyodor

So, let’s say my table name should be cars_all, how would the mix command look like to generate the context module, Ecto module and db table?

My problem is should it be:

$ mix phx.gen.context Cars_All Car_All cars_all ....

or

$ mix phx.gen.context Cars_all Car_all cars_all ....

or

$ mix phx.gen.context CarsAll CarAll cars_all ....

or something else?

stefanchrobot

stefanchrobot

Anything that works for you. The convention is to have:

  • Plural for context,
  • Singular for schema,
  • Plural for table name,
  • Singular for controller/view.

But you’re free to break away from the conventions anytime. For example, I store person records. While I could get away with “persons”, I chose to use “people” where I see fit:

  • Context: People,
  • Schema: Person,
  • Table: people,
  • Controller/view: Person.
al2o3cr

al2o3cr

mix phx.gen.context YouCanNameThese WhateverYouWant mumbles --table cars_all

There’s no requirement at all for those names, apart from the first two being a valid Elixir module name.

The convention is to spell those with UpperCamelCase but there’s no need to do so.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

Other popular topics Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement