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
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
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
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
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.
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








