Install Phoenix with Ecto2?

I am new to phoenix framework , i would like to know how to install phoenix app with ecto version 2

1 Like

Which version of phoenix are you using?
With the recent version of phoenix, it usually integrated with ecto_phoenix 3.0 with already depended on ecto 2.0.
If you has use --no-ecto to create a phoenix app there are guide on readme https://github.com/phoenixframework/phoenix_ecto to install it.

Dev.

@blisscs currently my phoenix_ecto version is 3.0 only, the reason i want to downgrade ecto is to work with mongodb where ecto3 not suporting mongodb

I haven’t heard that ecto 3 is out. Now it is ecto 2.x but ecto_phoenix is 3.x. I think you misunderstood something. I haven’t tried Ecto with mongodb yet. If I find info about this will add it here.

There is no reason to downgrade anything. Phoenix_ecto is a glue lib which takes the burden of boiler plating your app. It comes with a dependency on ecto 2.x. Which version is depended on exactly can be seen on hex.pm

It seems like for mongodb you would need ecto ~> 1.0

@idi527 now they have an updated repo with mongodb ecto adapter updated to v 2.0 ecto2

Then you are good to go with phoenix_ecto 3.0, I think.

again getting error like

mix deps.get
Running dependency resolution...

Failed to use "ecto" (version 2.1.4) because
mongodb_ecto (versions 0.1.0 to 0.1.2) requires ~> 1.0
phoenix_ecto (version 3.2.3) requires ~> 2.1
mix.lock specifies 2.1.4


Failed to use "ecto" (version 2.1.4) because
mongodb_ecto (versions 0.1.3 to 0.1.5) requires ~> 1.0.0
phoenix_ecto (version 3.2.3) requires ~> 2.1
mix.lock specifies 2.1.4

Can you post your mix.exs file?

I think you are using the old branch of mongodb adapter. Try specifying the ecto2 branch

defp deps do
  [...
   {:mongodb_ecto, github: "michalmuskala/mongodb_ecto", branch: "ecto-2"},
   ...]
end
defp deps do
[
 {:phoenix, "~> 1.2.1"},
 {:phoenix_pubsub, "~> 1.0"},
 {:phoenix_ecto, "~> 3.0"},
 {:postgrex, ">= 0.0.0"},
 {:phoenix_html, "~> 2.6"},
 {:phoenix_live_reload, "~> 1.0", only: :dev},
 {:gettext, "~> 0.11"},
 {:cowboy, "~> 1.0"},
 {:mongodb_ecto, github: "michalmuskala/mongodb_ecto", branch: "ecto-2"},
 ]
end

getting error like follow

* Updating mongodb_ecto (https://github.com/michalmuskala/mongodb_ecto.git)
Running dependency resolution...

Failed to use "ecto" (versions 2.0.0 to 2.0.6) because
deps/mongodb_ecto/mix.exs requires ~> 2.0.0
phoenix_ecto (version 3.2.3) requires ~> 2.1

** (Mix) Hex dependency resolution failed, relax the version requirements of your dependencies or unlock them (by 
using mix deps.update or mix deps.unlock). If you are unable to resolve the conflicts you can try overriding with 
{:dependency, "~> 1.0", override: true}

Now you need to decide whether to use ecto 2.1 or 2.0.
You can go with either by overriding the ecto’s version in your mixfile

defp deps do
  [{:phoenix, "~> 1.2.1"},
   {:phoenix_pubsub, "~> 1.0"},
   {:phoenix_ecto, "~> 3.0"},
   {:postgrex, ">= 0.0.0"},
   {:phoenix_html, "~> 2.6"},
   {:phoenix_live_reload, "~> 1.0", only: :dev},
   {:gettext, "~> 0.11"},
   {:cowboy, "~> 1.0"},
   {:mongodb_ecto, github: "michalmuskala/mongodb_ecto", branch: "ecto-2"},
   {:ecto, "~> 2.1", override: true}] # like this
end

You might not need postgrex btw if you are not using postgres.

now when i start server i am getting error like follow

Could not start application test_app: TestApp.start(:normal, []) returned an error: shutdown: failed to start child: TestApp.Repo
** (EXIT) an exception was raised:
    ** (UndefinedFunctionError) function Mongo.Ecto.child_spec/2 is undefined or private
        (mongodb_ecto) Mongo.Ecto.child_spec(TestApp.Repo, [otp_app: :test_app, repo: TestApp.Repo, database: "ecto_simple", hostname: "localhost"])
        (ecto) lib/ecto/repo/supervisor.ex:109: Ecto.Repo.Supervisor.init/1
        (stdlib) supervisor.erl:294: :supervisor.init/1
        (stdlib) gen_server.erl:328: :gen_server.init_it/6
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

my test_app.ex

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

# Define workers and child supervisors to be supervised
children = [
  # Start the Ecto repository
  supervisor(TestApp.Repo, []),
  # Start the endpoint when the application starts
  supervisor(TestApp.Endpoint, []),
  # Start your own worker by calling: TestApp.Worker.start_link(arg1, arg2, arg3)
  # worker(TestApp.Worker, [arg1, arg2, arg3]),
]

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

end

config.ex

config :test_app, TestApp.Repo,
database: "ecto_simple",
#username: "mongodb",
#password: "mongosb",
hostname: "localhost"

repo.ex
>defmodule TestApp.Repo do
use Ecto.Repo, otp_app: :test_app, adapter: Mongo.Ecto
end

mix.ex
>defp deps do
[{:phoenix, β€œ~> 1.2.1”},
{:phoenix_pubsub, β€œ~> 1.0”},
{:phoenix_ecto, β€œ~> 3.0”},
{:postgrex, β€œ>= 0.0.0”},
{:phoenix_html, β€œ~> 2.6”},
{:phoenix_live_reload, β€œ~> 1.0”, only: :dev},
{:gettext, β€œ~> 0.11”},
{:cowboy, β€œ~> 1.0”},
{:mongodb_ecto, β€œ~> 0.1”},
{:ecto, β€œ~> 2.1”, override: true},
]
end

You are still using ecto v1 adapter version from hex.pm {:mongodb_ecto, "~> 0.1"}.

To use mongodb adapter compatible with ecto v2 you need to get the code from github {:mongodb_ecto, github: "michalmuskala/mongodb_ecto", branch: "ecto-2"}.

I think deps from my previous post should work …

2 Likes

thanks man now server is running but when i trying to insert changeset from controller
changeset = User.changeset(%User{}, user_params)

case Repo.insert(changeset) do
  {:ok, _user} ->
    conn
    |> put_flash(:info, "User created successfully.")
    |> redirect(to: user_path(conn, :index))
  {:error, changeset} ->
    render(conn, "new.html", changeset: changeset)
end

getting error like function Mongo.Ecto.insert/6 is undefined or private

Seems like mongo ecto adapter only has insert/5 defined in Mongo.Ecto

Maybe try using ecto ~> 2.0.0

defp deps do
  [{:phoenix, "~> 1.2.1"},
   {:phoenix_pubsub, "~> 1.0"},
   {:phoenix_ecto, "~> 3.0"},
   {:postgrex, ">= 0.0.0"},
   {:phoenix_html, "~> 2.6"},
   {:phoenix_live_reload, "~> 1.0", only: :dev},
   {:gettext, "~> 0.11"},
   {:cowboy, "~> 1.0"},
   {:mongodb_ecto, github: "michalmuskala/mongodb_ecto", branch: "ecto-2"},
   {:ecto, "~> 2.0.0", override: true}] # <---
end
1 Like

thank u so much man, its works fine now