Ecto: (KeyError) key :otp_app not found in: []

I’m attempting to create a simple registry on postgres using Ecto but I get the error ** (KeyError) key :otp_app not found in: [] (elixir 1.16.3) lib/keyword.ex:599: Keyword.fetch!/2 (ecto 3.11.2) lib/ecto/repo/supervisor.ex:53: Ecto.Repo.Supervisor.compile_config/2 lib/my_app/trans_insert.ex:2: (module)
my config files looks ok and I followed the hexdocs ectos page
config.exs

import Config

config :testes_pay, MyApp.Repo,
  database: "friends",
  username: "pedri",
  password: "1234",
  port: 8080,
  hostname: "localhost"

repo.ex

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :testes_pay,
    adapter: Ecto.Adapters.Postgres
end

application.ex

defmodule TestesPay.Application do

  use Application

  def start(_type, _args) do
    Dotenv.load()
    children = [
      MyApp.Repo,
    ]

    opts = [strategy: :one_for_one, name: Myapp.Supervisor]  # Add a space after name:
    Supervisor.start_link(children, opts)
  end
end

I personally don’t use Ecto quite this way myself, but there’s one piece I don’t see mentioned. Is the application in mix.exs something like:

 def project do
    [
      app: :testes_pay
     <etc>
    ]
  end

I believe this application name has to be consistent with the entry:

I didnt understood the problem

What does your mix.exs look like?

I thought I sent, here is its

defmodule TestesPay.MixProject do
  use Mix.Project

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

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger],
      mod: {TestesPay.Application, []}
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:req, "~> 0.5.0"},
      {:ecto_sql, "~> 3.6"},
      {:postgrex, ">= 0.0.0"},
      {:rustler, "~> 0.34.0"},
      {:dotenv, "~> 3.0"},
      {:decimal, "~> 2.0"}
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
    ]
  end
end

Thanks. My initial thought wasn’t correct (and perhaps unsurprisingly so looking more closely at the error), but still, not a bad idea to be sure things are consistent.

I see one other thing from the Ecto documentation that I don’t see in your configurations:

There’s one final bit of configuration that we’ll need to add ourselves, since the generator does not add it. Underneath the configuration in config/config.exs, add this line:

config :friends, ecto_repos: [Friends.Repo]

copy

This tells our application about the repo, which will allow us to run commands such as mix ecto.create very soon.

(Getting Started — Ecto v3.12.3)

In your case I would expect this to look something like:

config :testes_pay, ecto_repos: [MyApp.Repo]

All my stuff uses dynamic repos so right now, so I could still be going down the wrong path since I don’t use the Ecto related configuration points. But it might be worth a look at that documentation page just to be sure everything is there.

I had this line added, it doesnt make it :frowning: there is smt similar to ecto?

I’m using Ecto, but my application doesn’t have well defined repositories at compile time; repositories are started by calling the equivalent of MyApp.Repo.start_link/1 and using configurations supplied at runtime for the option parameters (database, hostname, etc.) Basically it’s described at: Replicas and dynamic repositories — Ecto v3.12.3

Someone with better experience with the configuration route might be better placed than me to help (I was hoping it was just a simple missing config or something). But I can see if I can duplicate what you’re seeing this evening.

Is your postgres really running on port 8080?

1 Like

yes, is a docker container mapped through 8080 → 5432 tcp

1 Like

I just speed-ran setting up a little stock Phoenix Application complete with Ecto and the used the authentication generator to create migrations, etc. (naturally I set up the database and users to use). And everything just worked without really anything different than what you’re doing.

So… looking at the error more closely. You aren’t getting (or at least reporting that you’re getting) postgres errors or Repo startup errors. It looks like an error at some point later: lib/my_app/trans_insert.ex:2 Is this file something you can share?

sure

defmodule MyApp.ContractInserter do
  use Ecto.Repo

  def insert_random_contract do
    contract_params = %{
      chain: String.random(10),
      coin: String.random(10),
      transaction_value: Decimal.new(rand(1000000), 3),
      transaction_charged: Decimal.new(rand(1000000), 3),
      ref: String.random(20),
      payload: String.random(100),
      public: String.random(100),
      private: String.random(100),
      status: false
    }

    Repo.insert(MyApp.Schemas.Contract, contract_params)
  end
end

al this file do is to pass data and opts to Repo.insert
I’m also getting this error when I try to run CLI commands such as mix ecto.create

Now we’re getting somewhere. I think this is getting to the root of the issue that you originally posted. The line:

You’ve already created a repo module (MyApp.Repo), but here you’re setting up this module to be one, too, instead of just using the one you created. This explains the error you’re getting: use Ecto.Repo wants some keyword list options including :otp_app. And look at the error message you posted:

(KeyError) key :otp_app not found in: []

That doesn’t exist for the use line in this file… in fact you’re not passing any parameters in this case.

So in module MyApp.ContractInserter change line:

use Ecto.Repo

to

alias MyApp.Repo

That should at least resolve that specific error message.

3 Likes