Ecto: Migrations fail: function add/1 undefined

Created on 10 Sep 2015  路  3Comments  路  Source: elixir-ecto/ecto

Using ecto within a phoenix framework project, a simple migration fails, stating that add/1 is undefined. I've done a bit of reading, and I can see other elixir examples where a function needs to be imported, but in the context of ecto migrations, I haven't found any examples that do so.

I've included the output of "mix deps" below; is this an issue, or have I done something incorrect in the migration?

Based on the output, it looks like there hasn't been an attempt to make a database connection, but I can note that "mix ecto.create" worked fine, and I can see the new databases in the pgAdmin tool.

defmodule HelloPhoenix.Repo.Migrations.CreateContacts do
  use Ecto.Migration

  def up do
    create table(:contacts) do
      add :name
      add :phone

      timestamps
    end
  end

  def down do
    drop table(:contacts)
  end
end
C:\>mix ecto.migrate
** (CompileError) _build/dev/lib/hello_phoenix/priv/repo/migrations/20150910035317_create_contacts.exs:6: function add/1 undefined
    (stdlib) lists.erl:1336: :lists.foreach/2
    (stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
    (elixir) lib/code.ex:278: Code.load_file/2
    (ecto) lib/ecto/migrator.ex:213: anonymous fn/4 in Ecto.Migrator.migrate/4
    (elixir) lib/enum.ex:977: anonymous fn/3 in Enum.map/2
    (elixir) lib/enum.ex:1261: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir) lib/enum.ex:977: Enum.map/2
    (ecto) lib/mix/tasks/ecto.migrate.ex:61: Mix.Tasks.Ecto.Migrate.run/2
    (mix) lib/mix/cli.ex:55: Mix.CLI.run_task/2
    (stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
    (elixir) lib/code.ex:131: Code.eval_string/3
C:\> mix deps
* ranch 1.1.0 (Hex package)
  locked at 1.1.0 (ranch)
  ok
* poolboy 1.5.1 (Hex package)
  locked at 1.5.1 (poolboy)
  ok
* decimal 1.1.0 (Hex package)
  locked at 1.1.0 (decimal)
  ok
* poison 1.5.0 (Hex package)
  locked at 1.5.0 (poison)
  ok
* cowlib 1.0.1 (Hex package)
  locked at 1.0.1 (cowlib)
  ok
* cowboy 1.0.2 (Hex package)
  locked at 1.0.2 (cowboy)
  ok
* plug 1.0.0 (Hex package)
  locked at 1.0.0 (plug)
  ok
* phoenix_html 2.2.0 (Hex package)
  locked at 2.2.0 (phoenix_html)
  ok
* phoenix 1.0.2 (Hex package)
  locked at 1.0.2 (phoenix)
  ok
* postgrex 0.9.1 (Hex package)
  locked at 0.9.1 (postgrex)
  ok
* ecto 1.0.2 (Hex package)
  locked at 1.0.2 (ecto)
  ok
* phoenix_ecto 1.2.0 (Hex package)
  locked at 1.2.0 (phoenix_ecto)
  ok

Most helpful comment

Maybe the docs you are reading are outdated but we decided to require the type because getting the wrong type in the migration causes very confusing error messages. We allow the type to be skipped in the model though because the error messages with type mismatches there are at least clear.

All 3 comments

It must be add :field, :type.

Maybe the docs you are reading are outdated but we decided to require the type because getting the wrong type in the migration causes very confusing error messages. We allow the type to be skipped in the model though because the error messages with type mismatches there are at least clear.

Yes, an outdated page. Thank you for looking at this so quickly.

Was this page helpful?
0 / 5 - 0 ratings