5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

(Postgrex.Error) ERROR 42P07 (duplicate_table) relation "users" already exists (Elixir)

Posted at

はじめに

  • Phoenixプロジェクトで、mix testすると下記のようなエラーに遭遇しました
$ mix test
2020-08-24 21:51:45.411 JST [20850] ERROR:  database "hello_test" already exists
2020-08-24 21:51:45.411 JST [20850] STATEMENT:  CREATE DATABASE "hello_test" ENCODING 'UTF8'
2020-08-24 21:51:45.510 JST [20852] ERROR:  relation "users" already exists
2020-08-24 21:51:45.510 JST [20852] STATEMENT:  CREATE TABLE "users" ("id" bigserial, "name" varchar(255), "email" varchar(255), "bio" varchar(255), "number_of_pets" integer, "inserted_at" timestamp(0) NOT NULL, "updated_at" timestamp(0) NOT NULL, PRIMARY KEY ("id"))
** (Postgrex.Error) ERROR 42P07 (duplicate_table) relation "users" already exists
    (ecto_sql 3.4.5) lib/ecto/adapters/sql.ex:593: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir 1.10.4) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql 3.4.5) lib/ecto/adapters/sql.ex:686: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql 3.4.5) lib/ecto/migration/runner.ex:343: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql 3.4.5) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
    (elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql 3.4.5) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
    (stdlib 3.13) timer.erl:166: :timer.tc/1

解決策

$ MIX_ENV=test mix ecto.drop
  • mix ecto.dropmix ecto.resetは試してみたのだけどなー と右往左往していました
  • MIX_ENV=testを指定することで、"hello_test"のほうを対象にDROPしてくれます

原因(再現)

$ mix phx.new hello
$ cd hello
$ mix phx.gen.context Accounts User users name:string email:string:unique password:string
$ mix test
  • Phoenix Guide 日本語訳の一部をずいぶん以前にやって、それから久しぶりに違うところをやってみようとphx.newから同じ名前helloでプロジェクトを作ったところ遭遇しました
  • usersテーブルの内容がそれぞれ違っています
$ mix phx.new hello
$ cd hello
$ mix phx.gen.schema User users name:string email:string bio:string number_of_pets:integer
$ mix test
2020-08-24 21:51:45.411 JST [20850] ERROR:  database "hello_test" already exists
2020-08-24 21:51:45.411 JST [20850] STATEMENT:  CREATE DATABASE "hello_test" ENCODING 'UTF8'
2020-08-24 21:51:45.510 JST [20852] ERROR:  relation "users" already exists
2020-08-24 21:51:45.510 JST [20852] STATEMENT:  CREATE TABLE "users" ("id" bigserial, "name" varchar(255), "email" varchar(255), "bio" varchar(255), "number_of_pets" integer, "inserted_at" timestamp(0) NOT NULL, "updated_at" timestamp(0) NOT NULL, PRIMARY KEY ("id"))
** (Postgrex.Error) ERROR 42P07 (duplicate_table) relation "users" already exists
    (ecto_sql 3.4.5) lib/ecto/adapters/sql.ex:593: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir 1.10.4) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql 3.4.5) lib/ecto/adapters/sql.ex:686: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql 3.4.5) lib/ecto/migration/runner.ex:343: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql 3.4.5) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
    (elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql 3.4.5) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
    (stdlib 3.13) timer.erl:166: :timer.tc/1
  • わかってしまえば、「なーんだ」って話ですが、遭遇したときはあれれ? となったので記録を残しておきます
    • 答えを教えてくださった@pojiro さんに感謝です :lgtm::lgtm::lgtm:
    • 私は、PostgreSQLの管理コンソールから強引にhello_testデータベースを消していました
      • 同じことと言えば同じですが、MIX_ENV=test mix ecto.dropのほうが筋がいい気がします
  • 2020/8/22に行われたkokura.ex#12での出来事です
    • オーガナイザーの@im_miolab さんに感謝です :qiitan::rocket::rocket:
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?