I am confused about the Schema setup,
I am setting up a new application and I want to seed files in it as well. I tried to mix to create the schema mix phx.gen.html Delish.food delish_foods title:string ingredients:string summary:string date:integer
I keep getting this error even if I try phx.gen.schema
, the error looks like this
** (Mix) Expected the context, "Delish.food", to be a valid module name
mix phx.gen.html, phx.gen.json, phx.gen.live, and phx.gen.context
expect a context module name, followed by singular and plural names
of the generated resource, ending with any number of attributes.
For example:
mix phx.gen.html Accounts User users name:string
mix phx.gen.json Accounts User users name:string
mix phx.gen.live Accounts User users name:string
mix phx.gen.context Accounts User users name:string
The context serves as the API boundary for the given resource.
Multiple resources may belong to a context and a resource may be
split over distinct contexts (such as Accounts.User and Payments.User).
Do I just set up the schema with one title:string
?
2 Likes
Corresponding tweet for this thread:
Share link for this tweet.
2 Likes
mix phx.gen.html - a command that will generate Context module and Schema
https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Html.html
while
mix phx.gen.schema - will generate Schema
https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Schema.html
P.S. Sorry, posted too early. You already mentioned using mix pxh.gen.schema
P.P.S I think it’s supposed to be mix phx.gen.schema Delish.Food - ‘food’ should be started with a capital latter to indicate a module.
2 Likes
I am still getting the same error
** (Mix) Expected the schema, "delish_foods", to be a valid module name
mix phx.gen.html, phx.gen.json, phx.gen.live, and phx.gen.context
expect a context module name, followed by singular and plural names
of the generated resource, ending with any number of attributes.
For example:
mix phx.gen.html Accounts User users name:string
mix phx.gen.json Accounts User users name:string
mix phx.gen.live Accounts User users name:string
mix phx.gen.context Accounts User users name:string
The context serves as the API boundary for the given resource.
Multiple resources may belong to a context and a resource may be
split over distinct contexts (such as Accounts.User and Payments.User).
2 Likes
Could you give me an example using this mix phx.gen.html Delish.food delish_foods title:string ingredients:string summary:string date:integer
? on how the two mix commands are different?
2 Likes
I see. I got a bit confused and thought you wanted to only create Schema, but you’re trying to create Context and Schema.
mix phx.gen.html Foods Food delish_foods title:string ingredients:string summary:string date:integer
Foods - Context Module
Food - Schemade module
delish_foods - table name
It will create:
- a context module in
lib/app/foods.ex
for the accounts API
- lib/app/foods - folder
- lib/app/foods/food.ex - schema module
It will also create templates, views, and a controller. It will show all the files it created in the terminal.
3 Likes
Thank you so much yes this worked, what was I doing wrong?
2 Likes
You forgot about the Context Module.
mix phx.gen.html <Context module> <Schema Module> <plural table name> <list of attributes>
2 Likes
What is Context Module is the main defmodule? the Schema Module is the blueprint for the data
1 Like