kalkatfyodor

kalkatfyodor

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?

Showing Posts 1 to 10

LostKobrakai

LostKobrakai

The docs show the --table option to customize the table name: mix phx.gen.context — Phoenix v1.6.4

Though the generators are not general purpose code generators. They’re meant to get you started, but not to be and end to all code generation needs. So you’ll still run into cases, where you need to manually adjust what the generators supply or even ditch the generators completely for more custom needs.

kalkatfyodor

kalkatfyodor OP

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

Are you asking about the command or the common naming convention? How would you want to have the context, schema and table to be named?

kalkatfyodor

kalkatfyodor OP

Convention.

Especially for those long DB tables like: cars_from_alternative_universe

So, let’s say: with the table cars it’s trivial and it would be:

  1. $ mix phx.gen.context Cars Car cars alias:string etc...

But how would you do it for these cases?:

  1. cars_all
  2. cars_from_alternative_universe
  3. men_women_children
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.
kalkatfyodor

kalkatfyodor OP

Please, try with my three examples if possible ;):

  1. cars_all
  2. cars_from_alternative_universe
  3. men_women_children

Also, it would be cool if you could use this notation if possible:

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

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.

kalkatfyodor

kalkatfyodor OP

How would you do it with aforementioned examples of mine?:

  1. cars_all
  2. cars_from_alternative_universe
  3. men_women_children
stefanchrobot

stefanchrobot

I don’t think we can give you any useful tips without understanding what these entities model. For example, what is cars_all?

Why not have Cars context, Car schema and cars table? If cars is already taken, what’s the difference between cars and cars_all?

Same goes for the other two examples.

pdgonzalez872

pdgonzalez872

Hi @kalkatfyodor!

I think I see what you are trying to ask. Let me give it a go. Let’s first see what the generator uses (using your example):

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

Let’s take each of the args and analyze them:

  • mix → tool that runs the command
  • phx.gen.context → name of the task
  • Cars → name of the context
  • Car → name of the schema
  • cars → name of the db table
  • model:string → column + type
  • price:string → column + type

I created a dummy Phoenix app with mix phx.new hi. If you run the above command, you’d end up with the following diff.

I won’t focus on why or why not you’d want to do that (XY problem) as I think there is a lot of learning when one chases the X :slight_smile: . With that said, let’s try to answer what you asked. Your original question was:

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

Let’s assume you want to share the same context of Cars and want to tackle cars_all:

mix phx.gen.context Cars CarAll cars_all model:string price:string

Let’s take each of the args and analyze them:

  • mix → tool that runs the command
  • phx.gen.context → name of the task
  • Cars → name of the context, the generator will change the file to add the new functions
  • CarAll → name of the schema (this just needs to be a valid CamelCaseModuleName
  • cars_all → name of the db table
  • model:string → column + type
  • price:string → column + type

If you run the above command, you’d end up with the following diff.

I won’t show the last case as it is identical to this last case.

I hope this helps you with your original question! The generators are very powerful and instill good practices we find valuable in the community. I hope you saw the context concept here, how the generator creates a module to serve as the boundary for everything within that context. It’s great stuff.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews