elixir-ecto / ecto

A toolkit for data mapping and language integrated query.
https://hexdocs.pm/ecto
Apache License 2.0
6.18k stars 1.44k forks source link

Error on insert into database #330

Closed conradwt closed 9 years ago

conradwt commented 9 years ago

I'm seeing the following error when I attempt to insert into the database:

iex(7)> dweet = %EctoTest.Dweet{author: "conradwt", content: "This is a test" }
%EctoTest.Dweet{author: "conradwt", content: "This is a test", id: nil}
iex(8)> EctoTest.Repo.insert( dweet )
** (ArgumentError) repo EctoTest.Repo is not started
    (ecto) lib/ecto/adapters/postgres.ex:200: Ecto.Adapters.Postgres.repo_pool/1
    (ecto) lib/ecto/adapters/postgres.ex:139: Ecto.Adapters.Postgres.query/4
    (ecto) lib/ecto/adapters/postgres.ex:90: Ecto.Adapters.Postgres.insert/3
    (ecto) lib/ecto/repo/backend.ex:95: anonymous fn/4 in Ecto.Repo.Backend.insert/4

Next, I'm using Elixir 1.1.0-dev and :postgrex, "~> 0.5", :ecto, "~> 0.2.5".

josevalim commented 9 years ago

@conradwt you need to mount the repository in your supervision tree. We have mentioned it in the README and you can see an example in our simple app: https://github.com/elixir-lang/ecto/blob/master/examples/simple/lib/simple.ex#L4-L9 :)

conradwt commented 9 years ago

@josevalim I'm using the following unless tree within the above link has special meaning:

defmodule EctoTest do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # Define workers and child supervisors to be supervised
      # worker(EctoTest.Worker, [arg1, arg2, arg3])
      worker( EctoTest.Repo, [] )
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [ strategy: :one_for_one, name: EctoTest.Supervisor ]
    Supervisor.start_link( children, opts )
  end
end
josevalim commented 9 years ago

That should work too. You just need to specify the EctoTest module so your application is properly started:

https://github.com/elixir-lang/ecto/blob/master/examples/simple/mix.exs#L11

conradwt commented 9 years ago

@josevalim Thanks, I was missing the following within the application:

mod: { EctoTest, [] },
conradwt commented 9 years ago

Will it ever be possible for Mix to infer the top-level module within the mix.exs?

josevalim commented 9 years ago

I don't think that would be good in the long term. The application doesn't have to be in the top-level, you can have multiple app related modules and adds implicit behaviour which can be confusing. :)