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
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
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.
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’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.
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?
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.