ERROR 42P01 (undefined_table): relation "users" does not exist

I created a simple CRUD application with phoenix without problem, then I added coherence library, but when I try to insert a new user I receive the message below. I can run ecto.migration without problem, but when I look into database , the user table has not created. What can be? Thanks in advance.

I have not used the library personally, but you should probably check the docs for coherence. I assume they have some way of importing a migration to create the db table(s) they expect.

Hi Greg, IĀ“ve been taking a look at the docs and in the internet. What IĀ“ve found is that migrate would have resolved.

Do you have <timestamp>_add_coherence_to_user.exs file in your priv/repo/migrations/ directory?

No, only migrations and seeds.exs. Why ecto.migration isnĀ“t generating this file?

What do you mean?

Generating a migration for adding Coherence fields to a user table is a job for Coherence. How can Ecto know what do you intend? Itā€™s not a telepathic alien. :slight_smile:

Hi Dimitar, I only have a _create_coherence_to_user.exs.

defmodule TodoElixir.Repo.Migrations.CreateCoherenceUser do
  use Ecto.Migration
  def change do
    create table(:users) do

      add :name, :string
      add :email, :string
      # authenticatable
      add :password_hash, :string
      # recoverable
      add :reset_password_token, :string
      add :reset_password_sent_at, :utc_datetime
      # lockable
      add :failed_attempts, :integer, default: 0
      add :locked_at, :utc_datetime
      # trackable
      add :sign_in_count, :integer, default: 0
      add :current_sign_in_at, :utc_datetime
      add :last_sign_in_at, :utc_datetime
      add :current_sign_in_ip, :string
      add :last_sign_in_ip, :string
      # unlockable_with_token
      add :unlock_token, :string
      
      timestamps()
    end
    create unique_index(:users, [:email])

  end
end
 Is there anything wrong with this file? Thanks for help me

I have these files in migrations directory:

20200124072414_create_todos.exs			20200124073659_create_coherence_invitable.exs
20200124073658_create_coherence_user.exs	20200124232309_create_users.exs

That should be enough for the migration. What error message do you receive? You never specified it in your original post.

ERROR 42P01 (undefined_table): relation ā€œusersā€ does not exist

When I try to insert a new registration.

Okay, so post the output of this command for us:

mix do ecto.drop, ecto.create, ecto.migrate

Note that this will drop and re-create the database.


[info] == Running TodoElixir.Repo.Migrations.CreateTodos.change/0 forward
[info] create table todos
[info] == Migrated in 0.0s
[info] == Running TodoElixir.Repo.Migrations.CreateCoherenceUser.change/0 forward
[info] create table users
[info] create index users_email_index
[info] == Migrated in 0.0s
[info] == Running TodoElixir.Repo.Migrations.CreateCoherenceInvitable.change/0 forward
[info] create table invitations
[info] create index invitations_email_index
[info] create index invitations_token_index
[info] == Migrated in 0.0s
[info] == Running TodoElixir.Repo.Migrations.CreateUsers.change/0 forward
[info] create table users
** (Postgrex.Error) ERROR 42P07 (duplicate_table): relation "users" already exists
    (ecto) lib/ecto/adapters/sql.ex:200: Ecto.Adapters.SQL.query!/5
    (ecto) lib/ecto/adapters/postgres.ex:96: anonymous fn/4 in Ecto.Adapters.Postgres.execute_ddl/3
    (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/adapters/postgres.ex:96: Ecto.Adapters.Postgres.execute_ddl/3
    (ecto) lib/ecto/migration/runner.ex:104: anonymous fn/2 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto) lib/ecto/migration/runner.ex:102: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:181: :timer.tc/2
    (ecto) lib/ecto/migration/runner.ex:26: Ecto.Migration.Runner.run/6
    (ecto) lib/ecto/migrator.ex:128: Ecto.Migrator.attempt/6
    (ecto) lib/ecto/migrator.ex:73: anonymous fn/4 in Ecto.Migrator.do_up/4
    (ecto) lib/ecto/adapters/sql.ex:576: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
    (db_connection) lib/db_connection.ex:1283: DBConnection.transaction_run/4
    (db_connection) lib/db_connection.ex:1207: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:798: DBConnection.transaction/3
    (ecto) lib/ecto/migrator.ex:261: anonymous fn/4 in Ecto.Migrator.migrate/4
    (elixir) lib/enum.ex:1336: Enum."-map/2-lists^map/1-0-"/2
    (elixir) lib/enum.ex:1336: Enum."-map/2-lists^map/1-0-"/2
    (ecto) lib/mix/tasks/ecto.migrate.ex:83: anonymous fn/4 in Mix.Tasks.Ecto.Migrate.run/2
    (elixir) lib/enum.ex:783: Enum."-each/2-lists^foreach/1-0-"/2

Therefore show that table users is duplicated . I can insert a registration.
Thanks Dimiter

Yeah, you donā€™t need that one:

[info] == Running TodoElixir.Repo.Migrations.CreateUsers.change/0 forward

As you yourself have seen, you are trying to create the table in two migration scripts. You can just change the second one to add columns to your users table.

1 Like