Problem using context generators in umbrella app

H, Y’all!
I’m a bit lost in how to use context generators inside a Phoenix umbrella project.

I’ve created a new umbrella project using mix phx.new example --umbrella, which gives me two apps, example and example_web. Now, if I try to mix phx.gen.context Accounts User users in my example app directory I get an error saying ** (Mix) The task "phx.gen.context" could not be found. If I try the same on the root level of my umbrella I get told ** (Mix) mix phx.gen.context can only be run inside an application directory.

So how am I to run this inside the application directory?

You should run it from example_web. One way is to get the list of available mix options with

$ mix help

You will see that the task exists in app/example_web.

1 Like

That’s it, got it working now. Thanks a lot!

One thing I still don’t get though…

Say that I create a third app in my Apps directory, called testing by using mix phx.new.ecto testing. How would I go to add a new context with mix phx.gen.context into that app instead of the default example app (using this command inside example_web app)?

I tried mix phx.gen.context Testing.Accounts User users but that resulted a new module Testing created into the example app instead of creating this Accounts context inside my new testing app.

1 Like

You could go into apps/testing and use ecto generator instead…

$ cd apps/testing
$ mix ecto.gen.migration create_whatever
$ mkdir lib/testing/my_manual_context
$ touch lib/testing/my_manual_context.ex

There is not too much magic, just add the context folder, the context file, and some test files if needed.

Also for tests, You might have to configure the second sandbox.

Try this method
Add to deps in mix.exs after create mix phx.new.ecto testing

{:phoenix, "~> 1.6.2", only: :dev}

in config file

config : testing,
       ecto_repos: [Testing.Repo],
       generators: [
         context_app: :testing
       ]