Phx.gen.auth generated code fails test with duplicate table "users" already exists

Hi

I am working through the Pragmatic Programmer “Programming Phoenix LiveView” book, and in the code generation section we run the phx.gen.auth generator, but when I run the tests I get:

12:37:30.010 [info] extension “citext” already exists, skipping
** (Postgrex.Error) ERROR 42P07 (duplicate_table) relation “users” already exists
(ecto_sql 3.7.2) lib/ecto/adapters/sql.ex:760: Ecto.Adapters.SQL.raise_sql_call_error/1
(elixir 1.12.1) lib/enum.ex:1553: Enum."-map/2-lists^map/1-0-"/2
(ecto_sql 3.7.2) lib/ecto/adapters/sql.ex:852: Ecto.Adapters.SQL.execute_ddl/4
(ecto_sql 3.7.2) lib/ecto/migration/runner.ex:343: Ecto.Migration.Runner.log_and_execute_ddl/3
(ecto_sql 3.7.2) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
(elixir 1.12.1) lib/enum.ex:2356: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto_sql 3.7.2) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
(ecto_sql 3.7.2) lib/ecto/migration/runner.ex:280: Ecto.Migration.Runner.perform_operation/3

I can run mix ecto.reset

% mix ecto.reset
The database for Pento.Repo has been dropped
The database for Pento.Repo has been created

13:39:18.491 [info] == Running 20220209071806 Pento.Repo.Migrations.CreateUsersAuthTables.change/0 forward

13:39:18.501 [info] execute “CREATE EXTENSION IF NOT EXISTS citext”

13:39:18.631 [info] create table users

13:39:18.669 [info] create index users_email_index

13:39:18.671 [info] create table users_tokens

13:39:18.679 [info] create index users_tokens_user_id_index

13:39:18.680 [info] create index users_tokens_context_token_index

13:39:18.733 [info] == Migrated 20220209071806 in 0.2s

but the test fails with the same error. I’m not sure how to diagnose this as everything is pretty much generated code. Any ideas?

If you’re getting the error with tests, you should drop the test database and then try again:

MIX_ENV=test mix ecto.drop
mix test
2 Likes

well ok, that was awesome, thank you!