(RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist

Trying to call any Repo functions in any of the controllers would result in this error, wouldn’t even compile, compile successes if I comment any such calls.

== Compilation error in file lib/my_app_web/controllers/contact_controller.ex ==
** (RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist
    (ecto 3.9.4) lib/ecto/repo/registry.ex:22: Ecto.Repo.Registry.lookup/1
    (ecto 3.9.4) lib/ecto/repo/supervisor.ex:160: Ecto.Repo.Supervisor.tuplet/2
    lib/my_app/repo.ex:2: Vitalx.Repo.all/2
    lib/my_app_web/controllers/contact_controller.ex:11: (module)

Was working fine few minutes ago, all of a sudden this happens. Please advice on how to troubleshoot this.

Hi @AarKiMos welcome! Any time you have an error it will help a lot if you can post the code you have. Can you show the full controller module? I wonder if you are making a Repo call within the module body that’s being executed at compile time.

1 Like

Hi, I got it solved, will make sure to include code from next time.

I wonder if you are making a Repo call within the module body that’s being executed at compile time.

Was doing exactly this, was creating a module attribute with data fetched from database. Somehow ended up trying multiple calls from multiple contexts in multiple controllers all using module attributes only and getting the same error trying to troubleshoot this.

Made it into a private function :smiley:

----------- Errornous code

@contact_query_recipients Email.list_contact_query_subscribers()
                           |> Enum.map(fn s -> {s.name, s.email} end)

----------- Fixed code

  defp list_recipients do
    Email.list_contact_query_subscribers()
    |> Enum.map(fn s -> {s.name, s.email} end)
  end