Phoenix framework migration command creates files outside the lib directory

After creating a phoenix framework project the root directory contains the following directories.

README.md       _build/         assets/         config/         deps/           lib/            mix.exs         mix.lock        priv/           test/

If i run the following migration:
mix phoenix.gen.json Photo photos path:string
This creates a /web directory at the root path that lives outside the /lib directory but follows the same structure as ./lib/myproject_web

This does not make sense to me and doesn’t look like a correct structure. I would have though it should be merged into the structure inside the lib directory.

Can someone clarify if I am running the command wrong or this is indeed the correct file structure? thanks.

try mix phx.gen.json ContextName Photo photos path:string

mix phoenix.x is for phoenix 1.2 - mix phx.x is for phoenix >1.3 - judging from the root assets folder you used phx.new to create the project…

3 Likes

Thanks for setting me on the right path.

I am using phoenix >1.3

The command requires one more parameter which is the context module as documented here
https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Json.html

and it now looks like this in my case:

mix phx.gen.json API Photo photos path:string

I’m still thinking whether i need to redefine a new context as such, but the file generation, subsequent migration and GET /api/photos worked after adding this snippet in my router.ex

` scope “/api”, MyProjectWeb do
pipe_through :browser # Use the default browser stack

get "/photos", PhotoController, :index

end`

removing the create function from PhotoController (which wasn’t compiling) and executing the migration with:
mix ecto.migrate

Therefore end result is following request gives a 200
wget http://0.0.0.0:4000/api/photos

1 Like

good catch, forgot that part… have edited my answer with the correction…

OT: for the context part and figuring that out, check out this video Phoenix Contexts - learning resources - #12 by peerreynders - it’s a good intro to context mapping etc.

1 Like