Does Ecto require a supervision tree to work?

When I try to access records from a repo this is what I get. I created it mix new customers with the --sup. How do you add a supervision tree after you’ve created the app?

Sipusers.Repo
iex(2)> Alias Sipusers.Customers
** (SyntaxError) iex:2: syntax error before: Sipusers
    
iex(2)> alias Sipusers.Customers
Sipusers.Customers
iex(3)> Repo.all
** (UndefinedFunctionError) function Sipusers.Repo.all/0 is undefined or private. Did
 you mean one of:

      * all/1
      * all/2

    (customers) Sipusers.Repo.all()
iex(3)> Repo.all Customers
** (ArgumentError) repo Sipusers.Repo is not started, please ensure it is part of you
r supervision tree
    (ecto) lib/ecto/query/planner.ex:136: Ecto.Query.Planner.query_lookup/6
    (ecto) lib/ecto/query/planner.ex:119: Ecto.Query.Planner.query_with_cache/7
    (ecto) lib/ecto/repo/queryable.ex:122: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:35: Ecto.Repo.Queryable.all/4
iex(3)> Repo.all Customers
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution

Well the issue that this error is about has nothing to do with supervision and everything to do with the fact that there isn’t a Sipusers.Repo module at all. Do you have a module with that name? If so, where is it?

Since we see this error

** (UndefinedFunctionError) function Sipusers.Repo.all/0 is undefined or private. Did
 you mean one of:

      * all/1
      * all/2

    (customers) Sipusers.Repo.all()

I do think, that the module exists.

Ectos documentation describes pretty well, where to place it, along with where/how to find your supervisor.

Since you said you used --sup, you should have one. Just extend it as described in the docs

https://hexdocs.pm/ecto/getting-started.html#adding-ecto-to-an-application

It will propably be Sipusers,Supervisor in lib/sipusers/supervisor.ex, but thats only a guess and based on ancient memory. I haven’t used the generators anymore for a long time…

1 Like

Here it is in repo.ex

defmodule Sipusers.Repo do
  use Ecto.Repo, otp_app: :customers
end

When I look in the example I am working from it looks like there should be a mod: {Customers, []} section in applications list of the def application do function but there is only an extra_applicationslist

defmodule Customers.Mixfile do
  use Mix.Project

  def project do
    [app: :customers,
     version: "0.1.0",
     elixir: "~> 1.4",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps()]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    # Specify extra applications you'll use from Erlang/Elixir
    [extra_applications: [:logger, :ecto, :postgrex]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:my_dep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    [
      {:ecto, "~> 2.0"},
      {:postgrex, "~> 0.11"}
    ]
  end
end

Ah of course, not fully awake it seems.

@vonH what is this line supposed to do: Repo.all. You aren’t passing a query to it.

As @nobbz says you do indeed need to have it in y our supervision tree. However you also need to pass an actual queryable to Repo.all as well.

That documentation isn’t quite up to date and targets the “old” way to specify deps. Assume this step as done and move on (but while we talk about it, you will probably stumble over this quite often, so you should really read about elixir 1.4s dependency inference).

The vital thing to do is described a bit later:

def start(_type, _args) do
  import Supervisor.Spec, warn: false

  children = [
    supervisor(Friends.Repo, []),
  ]

  ...