Can't create test database for my phoenix app

Hi. I’m trying to run my tests for the first time in my project. But for some reason I can’t get the test database created. Every time I run

 MIX_ENV=test mix ecto.migrate

or

mix test

I get at the end this error:

** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "clubs" does not exist
    (ecto_sql) lib/ecto/adapters/sql.ex:605: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql) lib/ecto/adapters/sql.ex:692: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql) lib/ecto/migration/runner.ex:302: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql) lib/ecto/migration/runner.ex:111: anonymous fn/5 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql) lib/ecto/migration/runner.ex:110: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:166: :timer.tc/1

The migrate command works fine for the dev database. Not sure why it only fails for test.

Did You create the database? :slight_smile:

MIX_ENV=test mix ecto.create

No. If I run create and then migrate, it stills fails:

➜ dummy_api git:(master) ✗ MIX_ENV=test mix ecto.create
The database for DummyApi.Repo has already been created
➜  dummy_api git:(master) ✗ MIX_ENV=test mix ecto.migrate
** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "clubs" does not exist
    (ecto_sql) lib/ecto/adapters/sql.ex:605: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql) lib/ecto/adapters/sql.ex:692: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql) lib/ecto/migration/runner.ex:302: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql) lib/ecto/migration/runner.ex:111: anonymous fn/5 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql) lib/ecto/migration/runner.ex:110: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:166: :timer.tc/1
    (ecto_sql) lib/ecto/migration/runner.ex:111: anonymous fn/5 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1940: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql) lib/ecto/migration/runner.ex:110: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:166: :timer.tc/1

I also tried to drop, create, migrate, but same result

I received this error. when my table name in the code doesn’t match the one with the database. Check your table name clubs. install some GUI for your database or use command line to see if data base is created or not.

1 Like

This is the error. Somewhere you’re running a migration on a table called clubs that doesn’t exist yet. Check your migrations individually to track down the error.

Some things I’ve done in the past that caused this:

  1. had a references to that table before defining that table
  2. added or modified a column to the table in a migration that happened in the wrong order
1 Like

Yeah, what happened was that the order in my migratios caused this. Had to renamed the timestamp in the migrations name, to force the framework into executing them in the desired order.

Thank you all guys for the help