shou.com
JP / EN

PG::DuplicateTable: ERROR:と表示されてしまった時の対処法

Wed Jul 26, 2017
Wed Jul 26, 2017

マイグレーションをしようとした時に下のようなエラーが表示された。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Mac:$ rake db:migrate
== 20170726082613 CreateGamble: migrating =====================================
-- create_table(:gambles)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "gambles" already exists
: CREATE TABLE "gambles" ("id" serial primary key, "a_team" character varying, "b_team" character varying, "game" timestamp, "event" character varying, "place" character varying, "description" character varying)
〜省略〜
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

このエラーの意味は、もうすでに同じtableがあるんですよと言った感じのエラー。

この状況になったら単純にtableを削除してあげたら問題ない。

ターミナルで下のように入力する。

1
$ rails db

その後、#が表示されるので、そこで\dと入力する。

1
2
3
4
5
6
7
8
petipeti_development=# \d
               List of relations
 Schema |       Name        |   Type   | Owner
--------+-------------------+----------+-------
 public | gambles           | table    | boku
 public | gambles_id_seq    | sequence | boku
 public | schema_migrations | table    | boku
(3 rows)

今回の場合は、NameがgamblesでTypeがtableのやつを消したいので、下のよう入力する。

1
petipeti_development=# drop table gambles;

ちゃんとできたかの確認。

1
2
3
4
5
6
petipeti_development=# \d
             List of relations
 Schema |       Name        | Type  | Owner
--------+-------------------+-------+-------
 public | schema_migrations | table | boku
(1 row)

うまくいっていますので、あとは、

1
$ rake db:migrate

だけでok

See Also